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.

Answer from enzotib on askubuntu.com
Discussions

linux - Copying a list of files - Unix & Linux Stack Exchange
I have a directory which has plenty of files which need to be copied over to another directory. Remember, not all files need to be copied, only some of them. Is there a way I can come up with a scr... More on unix.stackexchange.com
🌐 unix.stackexchange.com
June 26, 2019
cp - How do I create a copy of a directory in Unix/Linux? - Stack Overflow
I want to recursively create a copy of a directory and all its contents (e.g. files and subdirectories). More on stackoverflow.com
🌐 stackoverflow.com
copying all file
I'm not sure if you're looking for a better way to do this, or just an explanation about why it doesn't work. A simple way to accomplish what you want is to use * instead of .. At the end of your source directory. cp -i /source/* /destination As for why you're seeing the behavior you're seeing... Hopefully someone can help clear this up for me, because I got a bit confused while looking into it. The simple explanation would be that . represents a directory, and cp will not copy any directories without the -r option. That being said, cp doesn't really seem to handle it as a directory. This seems to be because the trailing . character isn't expanded by the shell, and is instead interpreted by cp internally. The way cp handles it is... weird. It seems like kind of a special case. Practically it can be used to specify hidden files in a recursive copy. cp -r /source/. /destination will copy all files in the source directory including hidden files. cp -r /source/* /destination will copy all files in the source directory excluding hidden files. In both cases the directory itself isn't copied, just the contents. In the example above the . does not seem to represent a directory. It behaves more like a special wildcard character that includes hidden files. That would indicate that cp doesn't consider it a directory, and so -r should not be required... I don't really understand WHY it behaves that way and I can't seem to find any good explanation for it anywhere. So good question. I'm stumped. Hopefully someone else can elaborate. More on reddit.com
🌐 r/linuxquestions
6
8
June 29, 2018
bash - Copy files from one directory into an existing directory - Stack Overflow
In bash I need to do this: take all files in a directory copy them into an existing directory How do I do this? I tried cp -r t1 t2 (both t1 and t2 are existing directories, t1 has files in it) bu... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › cp-command-linux-examples
cp Command in Linux - GeeksforGeeks
4 days ago - The cp command has a flexible syntax depending on whether you are copying a single file, multiple files, or directories. cp [options] <source> <destination> cp [options] <source1> <source2> ... <destination_directory> ... When the cp command ...
🌐
freeCodeCamp
freecodecamp.org › news › the-linux-cp-command-how-to-copy-files-in-linux
The Linux cp Command – How to Copy Files in Linux
June 6, 2022 - cp ./DirectoryA_1/README.txt ./DirectoryA_1/ANOTHER_FILE.txt ./DirectoryA_2 · As you can see, you will put all the source files first, and the last argument will be the destination.
Top answer
1 of 5
5

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/

2 of 5
1

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.txt contains the correct path and filename of that source file
  • the directory path you're copying the files to is test and 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.

Top answer
1 of 7
321

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.
2 of 7
108

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

Top answer
1 of 3
2518

The option you're looking for is -R.

cp -R path_to_source path_to_destination/
  • If destination doesn't exist, it will be created.
  • -R means copy directories recursively. You can also use -r since it's case-insensitive.
  • To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use -a flag along with trailing /. in the source (as per @muni764's / @Anton Krug's comment):
cp -a path_to_source/. path_to_destination/
2 of 3
393

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).

🌐
Reddit
reddit.com › r/linuxquestions › copying all file
r/linuxquestions on Reddit: copying all file
June 29, 2018 -

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,

Top answer
1 of 4
6
It is early and with one eye open look into “rsync”.
2 of 4
2
I'm not sure if you're looking for a better way to do this, or just an explanation about why it doesn't work. A simple way to accomplish what you want is to use * instead of .. At the end of your source directory. cp -i /source/* /destination As for why you're seeing the behavior you're seeing... Hopefully someone can help clear this up for me, because I got a bit confused while looking into it. The simple explanation would be that . represents a directory, and cp will not copy any directories without the -r option. That being said, cp doesn't really seem to handle it as a directory. This seems to be because the trailing . character isn't expanded by the shell, and is instead interpreted by cp internally. The way cp handles it is... weird. It seems like kind of a special case. Practically it can be used to specify hidden files in a recursive copy. cp -r /source/. /destination will copy all files in the source directory including hidden files. cp -r /source/* /destination will copy all files in the source directory excluding hidden files. In both cases the directory itself isn't copied, just the contents. In the example above the . does not seem to represent a directory. It behaves more like a special wildcard character that includes hidden files. That would indicate that cp doesn't consider it a directory, and so -r should not be required... I don't really understand WHY it behaves that way and I can't seem to find any good explanation for it anywhere. So good question. I'm stumped. Hopefully someone else can elaborate.
Find elsewhere
Top answer
1 of 4
30
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 dir2 is 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, leaving cp dir1/*. Which, depending on the expansion of dir1/*, may even destroy data:
      • If dir1/* matches only one file or directory, you will get an error from cp.
      • 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 dir2 is not empty, it again depends:
    • If the last match of dir2/* is a directory, dir1/* and the other matches of dir2/* will be moved into.
    • If the last match of dir2/* is a file, you probably will get an error message, unless dir1/* matches only one file.
2 of 4
16

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
🌐
FOSS Linux
fosslinux.com › home › learn linux › copying all files and folders to another directory in linux
Copying All Files and Folders to Another Directory in Linux
April 4, 2023 - In this guide, we’ll focus on copying all files from one directory to another using the cp command. The cp command is a simple yet powerful tool that allows you to copy files and directories in Linux.
🌐
Linode
linode.com › docs › guides › how-to-copy-files-and-directories-in-linux
How to Copy Files and Directories in Linux | Linode Docs
July 18, 2022 - The -r option enables the cp command to operate recursively and copy a directory along with any files and subdirectories it contains. cp has a number of options, allowing users to run it interactively, use verbose mode, or preserve the file ...
🌐
IBM
ibm.com › docs › en › aix › 7.2.0
Copying files (cp command)
Use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters.
🌐
Stanford
web.stanford.edu › class › archive › cs › cs107 › cs107.1186 › unixref › topics › cp
cp and mv (copy and move files and directories)
This will copy the entire assign0 directory into the directory called assign1. If assign1 does not exist, the cp command will create it. The mv command moves files in a similar way to cp.
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to copy files and directories in linux
How to Copy Files and Directories in Linux (With Examples)
December 19, 2025 - To copy more than one file at a time, list each file to be copied before entering the destination directory: cp my_file.txt my_file2.txt my_file3.txt path/to/destination · The system creates a copy of all three files in the destination folder.
🌐
Super User
superuser.com › questions › 717439 › how-to-copy-all-end-files-in-a-directory-to-another-directory-in-unix
linux - How to copy all end files in a directory to another directory in unix? - Super User
Currently, it copies all of the right items that are not in those two folders, but then it copies those folders themselves, when I need it to just copy their contents. ... You can pass the -type f flag to find (i.e. find ./ -type f) and it will find ONLY files...that being said, how comfortable are you with shell scripts as this could be attained with one?