You can copy the contents of a folder /source to another existing folder /dest with the command:
cp -a /source/. /dest/
The -a option is an improved recursive option, that preserves all file attributes and symlinks.
The . at end of the source path is a specific cp syntax that copies all files and folders, including hidden ones.
You can copy the contents of a folder /source to another existing folder /dest with the command:
cp -a /source/. /dest/
The -a option is an improved recursive option, that preserves all file attributes and symlinks.
The . at end of the source path is a specific cp syntax that copies all files and folders, including hidden ones.
An alternate is rsync:
rsync -a source/ destination
The advantages of rsync are:
- After the initial sync, it will then copy only the files that have changed.
- You can use it over a network, convenient for files in $HOME, especially config files.
Try this:
cp Folder1/* Folder2/
Quite simple, with a * wildcard.
cp -r Folder1/* Folder2/
But according to your example recursion is not needed so the following will suffice:
cp Folder1/* Folder2/
EDIT:
Or skip the mkdir Folder2 part and just run:
cp -r Folder1 Folder2
linux - Copying a list of files - Unix & Linux Stack Exchange
cp - How do I create a copy of a directory in Unix/Linux? - Stack Overflow
copying all file
bash - Copy files from one directory into an existing directory - Stack Overflow
Given your comment on user79914's response ("Unfortunately, the files do not have similar names…"), it sounds like you'll have to explicitly list the files you want copied in a regular old cp command. Something like the following should do the job:
cp FILE_1 FILE_2 FILE_3 /destination/directory
If, for instance, your copy operation is one you'll be doing more than once, you could do something like the following:
for FILE in $(cat ./list_of_files.txt)
do
cp ${FILE} /destination/directory
done
This example assumes that you've added the list of desired files to the text file list_of_files.txt. The benefit of this approach, especially if you're having to perform the copy more than once, is that you can just add any new files you need copied to your list_of_files.txt file.
For more examples like this one, check out this link:
https://www.cyberciti.biz/faq/bash-for-loop/
The simplest method doesn't require any scripting ability on your part. It only requires a text editor. Certainly there are more elegant and more generalized ways to do it, but sometimes one just wants to accomplish an immediate task without having to detour into a lot of learning curve climbing and such, if your scripting skills are at a novice level.
Let's suppose you have a file called myfiles.txt which contains a list of every filename you want to copy, one to a line. I'm going to assume:
- you have fewer than one million files in the list
- each line of
myfiles.txtcontains the correct path and filename of that source file - the directory path you're copying the files to is
testand it already exists - you have no problematic characters in your filenames like quotes, apostrophes, newlines and the like.
Now say:
vi myfiles.txt
:%s/^/cp -vp '/
:%s-$-' test/-
ZZ
That will edit your list of files to put cp -vp ' at the beginning of every line (every filename), and put ' test/ at the end of every filename.
You now have a list of N cp commands that will copy your N files into the test/ directory. You can execute it by saying:
sh < myfiles.txt
Voila.
Simply copy multiple files at once from command line
There are several ways you could achieve this. The easiest I have seen is to use the following.
cp /home/usr/dir/{file1,file2,file3,file4} /home/usr/destination/
The syntax uses the cp command followed by the path to the directory the desired files are located in with all the files you wish to copy wrapped in brackets and separated by commas.
Make sure to note that there are no spaces between the files. The last part of the command, /home/usr/destination/, is the directory you wish to copy the files into.
or if the all the files have the same prefix but different endings you could do something like this:
cp /home/usr/dir/file{1..4} ./
Where file1,file2,file3 and file4 would be copied.
From how you worded the question I believe this is what you're looking for but it also sounds like you might be looking for a command to read from a list of files and copy all of them to a certain directory. If that is the case let me know and i'll edit my answer.
Dealing with duplicates with python
So I wrote a little python script that I believe should get the job done. However, I am not sure how well versed you are in python (if versed at all) so I will try explaining how to use this script the best I can and please ask as many questions about it as you need.
import os,sys,shutil
### copies a list of files from source. handles duplicates.
def rename(file_name, dst, num=1):
#splits file name to add number distinction
(file_prefix, exstension) = os.path.splitext(file_name)
renamed = "%s(%d)%s" % (file_prefix,num,exstension)
#checks if renamed file exists. Renames file if it does exist.
if os.path.exists(dst + renamed):
return rename(file_name, dst, num + 1)
else:
return renamed
def copy_files(src,dst,file_list):
for files in file_list:
src_file_path = src + files
dst_file_path = dst + files
if os.path.exists(dst_file_path):
new_file_name = rename(files, dst)
dst_file_path = dst + new_file_name
print "Copying: " + dst_file_path
try:
shutil.copyfile(src_file_path,dst_file_path)
except IOError:
print src_file_path + " does not exist"
raw_input("Please, press enter to continue.")
def read_file(file_name):
f = open(file_name)
#reads each line of file (f), strips out extra whitespace and
#returns list with each line of the file being an element of the list
content = [x.strip() for x in f.readlines()]
f.close()
return content
src = sys.argv[1]
dst = sys.argv[2]
file_with_list = sys.argv[3]
copy_files(src,dst,read_file(file_with_list))
This script should be relatively simple to use. First off, copy the above code into the program gedit (should be pre-installed in Ubuntu) or any other text editor.
After that is complete, save the file as move.py in your home directory (it can be any directory but for ease of instruction lets just use the home directory) or add the directory the file is contained in to your PATH. Then cd to your home directory (or whatever directory you saved move.py in) from the terminal and type the following command:
python move.py /path/to/src/ /path/to/dst/ file.txt
This should copy all of the files that are listed from the source directory to the destination directory with duplicates taking the format pic(1).jpg, pic(2).jpg and so on. file.txt should be a file that lists all the pictures you would like to copy with each entry on its own separate line.
In no way should this script effect the source directory, however just make sure to enter the correct paths to the source and destination directory and the worst that could happen is you copy the files to the wrong directory.
Notes
- This script assumes that all of the original pictures are in the same directory. If you want it to check sub directories as well the script will need to be modified.
- If you accidentally mistype a file name, the script will spit out the error
"file does not exist" and prompt you to "press enter" to continue and the script will continue copying the rest of the list. - Don't forget the trailing
/on both the path to the source
directory and path to the destination directory. Otherwise the script will spit an error back at you.
Perhaps I'm missing a detail of your question, but the answers given seem excessive. If you want a command line solution and not a script, why not:
cd /path/to/src/
cp -t /path/to/dst/ file1 file2 file3 ...
The nice thing about doing it this way is that you can tab complete the file names
The option you're looking for is -R.
cp -R path_to_source path_to_destination/
- If
destinationdoesn't exist, it will be created. -Rmeanscopy directories recursively. You can also use-rsince it's case-insensitive.- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use
-aflag along with trailing/.in the source (as per@muni764's /@Anton Krug's comment):
cp -a path_to_source/. path_to_destination/
You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.
If the directory you're copying is called dir1 and you want to copy it to your /home/Pictures folder:
cp -r dir1/ ~/Pictures/
Linux is case-sensitive and also needs the / after each directory to know that it isn't a file. ~ is a special character in the terminal that automatically evaluates to the current user's home directory. If you need to know what directory you are in, use the command pwd.
When you don't know how to use a Linux command, there is a manual page that you can refer to by typing:
man [insert command here]
at a terminal prompt.
Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you've started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.
There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox) -R, -r, and --recursive are all equivalent, as mentioned in this answer. For portability, i.e. POSIX compliance, you would want to use -R because of some implementation-dependent differences with -r. It's important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).
Hey,
I wanted to copy all all files from one directory to another, like this:
cp -i /source/. /destination
I knew the source had no subdirectories and I wanted a prompt if files needed to be overwritten.
Now, this didnt work got the error " -r not specified, omitting directory /source/"
It worked with -r. I dont understand why though, there wre
Cheers,
Try
cp -R /var/www /media/magneto Avoid the extra forward slash at the end of the path name.
Or
you can navigate to the /var directory in terminal and then try
cp -R www /media/magneto
If the magneto directory exists, you can use the following command:
cp -R /var/www /media/magneto
If it doesn't already exist you can use the following command to create it:
rsync -av /var/www/ /media/magneto
What you want is:
cp -R t1/. t2/
The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.
cp dir1/* dir2
Or if you have directories inside dir1 that you'd want to copy as well
cp -r dir1/* dir2
cp dir1/* dir2
cp will not copy directories unless explicitly told to do so (with --recursive for example, see man cp).
Note 1: cp will most likely exit with a non-zero status, but the files will have been copied anyway. This may be an issue when chaining commands based on exit codes:&&, ||, if cp -r dir1/* dir2; then ..., etc. (Thanks to contrebis for their comment on that issue)
Note 2: cp expects the last parameter to be a single file name or directory. There really should be no wildcard * after the name of the target directory. dir2\* will be expanded by the shell just like dir1\*. Unexpected things will happen:
- If
dir2is empty and depending on your shell and settings:- you may just get an error message, which is the best case scenario.
dir2/*will be taken literally (looking for a file/directory named*), which will probably lead to an error, too, unless*actually exists.dir2/*it will just be removed from the command entirely, leavingcp dir1/*. Which, depending on the expansion ofdir1/*, may even destroy data:- If
dir1/*matches only one file or directory, you will get an error fromcp. - If
dir1/*matches exactly two files, one will be overwritten by the other (Bad). - If
dir/*matches multiple files and the last match is a, you will get an error message. - If the last match of
dir/*is a directory all other matches will be moved into it.
- If
- If
dir2is not empty, it again depends:- If the last match of
dir2/*is a directory,dir1/*and the other matches ofdir2/*will be moved into. - If the last match of
dir2/*is a file, you probably will get an error message, unlessdir1/*matches only one file.
- If the last match of
It's the shell that expands wildcards, not the commands. So cp dir1/* dir2/* first expands the two wildcards, then calls cp on the result. This is not at all what you apparently expect: depending on how many files there are already in dir2, dir2/* may expand to one or more argument. The command cp doesn't know which of its arguments came from expanding the first pattern and which ones came from expanding the second pattern. It expects its last argument to be the name of the destination directory. Thus, to copy all the files from the directory dir1 into the directory dir2, the last argument must be the directory dir2:
cp dir1/* dir2
Since * matches all files, cp attempts to copy all files. This includes directories: directories are files too. It skips directories, but reports an error. It copies the content of special files such as named pipes (something had better be writing to them, or cp will block), etc.
To copy only regular files, you need to restrict the matching. In zsh, you can use the glob qualifier . for that:
cp dir1/*(.) dir2
Other shells don't have this. You can use the find command to filter on file types. Assuming that you're running non-embedded Linux or Cygwin:
find dir1 -maxdepth 1 -type f -exec cp -t dir2 {} +
On Linux, FreeBSD and OSX:
find dir1 -maxdepth 1 -type f | xargs -I {} cp {} dir2
cp -r /path/to/someDirA/folderTwo /path/to/someDirB/
The -r option to cp tells it to recurse on directories, copying its contents.
You can use cp to copy files and directories:
cp -r /path/to/someDirA/folderTwo /path/to/someDirB
The -r option is needed when copying directories.
Alternatively, you can use rsync:
rsync -a /path/to/someDirA/folderTwo /path/to/someDirB