Bash: How to Copy All Files in Directory to Another Directory


Often you may want to copy all files from one directory to another using Bash.

You can use the following syntax to do so:

cp -R folder1/. folder2/

This particular example copies all files from the folder named folder1 to the folder named folder2.

Here is what the various commands do:

  • cp: This command allows you to copy one or more files to another location
  • -R: This stands for recursive, which allows you to recursively move down each folder and copy all contents.

Note that the dot . after folder1/ specifies that all contents of folder1 should be copied instead of just the folder itself.

The following example shows how to use this syntax in practice.

Example: How to Copy All Files in Directory to Another Directory

Suppose that we have a directory named folder1 with three text files and we would like to copy all of these text files into another directory named folder2.

We can use the ls command to view all of the files in both folders:

Suppose that we would like to copy all three text files from folder1 into folder2.

We can use the following syntax to do so:

cp -R folder1/. folder2/

The following screenshot shows how to use this syntax in practice:

Bash copy all files in directory to another directory

Notice that when we use the ls command again to view the contents of folder2 we can see that each of the files from folder1 have been copied into folder2.

Note #1: In this particular example we only copied files from one directory to another, but this syntax will also copy all folders from one directory to another.

Note #2: This method will also copy hidden files and folders.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to List All Files in Folder by Date
Bash: How to List All Files in Folder by Size
Bash: How to Delete All Files in Directory

Leave a Reply