How to Copy All Files from a Directory to another Directory in Linux

17

Copying the file or folder means creating a new file having duplicate content as in the existing file.

Sometimes, we need to copy the files or folders rather than having a backup program. The files can be copied with the same name, or you can change the name as well.

Copying a file, folder, or directory is a simple and basic task in the Linux operating system. Rename, delete or copy commands are used as daily purpose operations while working with the command-line interface.

Although there are multiple commands to copying the files, the “cp” and “rsync” command are widely used simplest approaches.

How to Copy Files with “cp” Command in Linux:

The “cp” command is one of the commonly used commands to perform the copy operation. You can copy files or folders from source to destination, i-e, one directory through this command.

The syntax of the “cp” command is:

$ cp [options] [source] [destination…]

Let’s take a look at an example to understand the “cp” command tool better.

In the home directory, create a “temp” folder with the text file named “text_file1.txt” and add random content to it.

Copy a file with the same name:

To copy a “text_file1.txt” file directory with the same name, open the terminal and type the mentioned “cp” command with the right path.

Get the folder’s path by right-clicking on the file and navigate to the “Properties” option (it is the easy way to get the path link).

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652989 581 How to Copy All Files from a Directory to another

A dialogue box will open with the complete path of a text file:

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652989 616 How to Copy All Files from a Directory to another

Use this path with the “cp” command to copy file:

$ cp /home/wardah/temp/text_file1.txt /home/wardah/temp2

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652990 306 How to Copy All Files from a Directory to another

This command will copy the “text_file1.txt” file to the “temp2” folder.

To verify it, type the “ls” command in the terminal:

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652990 115 How to Copy All Files from a Directory to another

Copy a file with a different name:

To copy the file in the current working directory with the different name, type the following “cp” command with file location:

$ cp /home/wardah/temp/text_file1.txt /home/wardah/temp/text_file2.txt

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652990 882 How to Copy All Files from a Directory to another

Verify it using the “ls” command”:

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652991 657 How to Copy All Files from a Directory to another

Copy Multiple Files with “cp” Command:

To copy multiple files with the “cp” command, navigate the terminal to the directory where files are saved and then run the “cp” command with the file names you want to copy and the destination path.

$ cd /home/wardah/temp

$ cp text_file1.txt text_file2.txt text_file3.txt /home/wardah/temp2

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652991 297 How to Copy All Files from a Directory to another

Run the mentioned command to verify if files are copied successfully:

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652991 888 How to Copy All Files from a Directory to another

Above mentioned scenarios are how to copy a single or selected file in a directory. Now, use the wildcard character (*) to copy present files of one directory to any other specific directory.

Let’s check how it works:

Run the “ls” command to check how many files exist in the temp directory:

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652992 156 How to Copy All Files from a Directory to another

Instead of mentioning all file names in the terminal, use the wildcard (*) with the directory path to copy all the files into destination:

$ cp /home/wardah/temp/* /home/wardah/temp2

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652992 788 How to Copy All Files from a Directory to another

Now, run the “ls” command again to check if all files are copied in the “temp2” directory:

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652992 866 How to Copy All Files from a Directory to another

How to Copy files with the “rsync” command in Linux:

The “rsync” command is another versatile Linux tool to synchronize and copy files and directories locally as well as remotely.

The syntax of the “rsync” command is to copy files is:

$ rsync [options…] [source] [destination…]

It is a pre-built tool in many Linux distribution. However, if you don’t get it on your system, install it by executing the following command:

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652993 820 How to Copy All Files from a Directory to another

To copy a file from one place to another, run the following command:

$ rsync /home/wardah/dir1/file1.txt /home/wardah/dir2/text_file.txt

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652993 47 How to Copy All Files from a Directory to another

To confirm, type:

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652994 26 How to Copy All Files from a Directory to another

To copy all the directory files to another location, the command would be:

$ rsync -a /home/wardah/dir1/ /home/wardah/dir2

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652994 393 How to Copy All Files from a Directory to another

(The “-a” with the “rsync” command is used to copy directories recursively)

Here are the two concepts:

If you add a trailing slash (/) with the path, it will copy the content of the source directory to the destination directory, just like shown in the image:

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652994 132 How to Copy All Files from a Directory to another

But, if you don’t add it, it will copy the source directory inside the destination directory, like:

$ rsync -a /home/wardah/dir /home/wardah/dir2

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652995 144 How to Copy All Files from a Directory to another

The above command will copy a “dir1” directory to the “dir2” directory.

how to copy all files from a directory to another directory in linux How to Copy All Files from a Directory to another Directory in Linux 1623652995 999 How to Copy All Files from a Directory to another

Conclusion:

Copying a file or directory is the basic command one can operate. One can use it multiple times while using Linux operating system.

This guide has seen the two simplest approaches, the “cp” command and the “rsync” command. Using these commands, we have learned how to copy a single file, multiple files, and even copy one directory to another.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More