How to Copy All Files from a Directory to another Directory in Linux
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).
A dialogue box will open with the complete path of a text file:
Use this path with the “cp” command to copy file:
$ cp /home/wardah/temp/text_file1.txt /home/wardah/temp2
This command will copy the “text_file1.txt” file to the “temp2” folder.
To verify it, type the “ls” command in the terminal:
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
Verify it using the “ls” command”:
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.
$ cp text_file1.txt text_file2.txt text_file3.txt /home/wardah/temp2
Run the mentioned command to verify if files are copied successfully:
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:
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
Now, run the “ls” command again to check if all files are copied in the “temp2” directory:
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:
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
To confirm, type:
To copy all the directory files to another location, the command would be:
$ rsync -a /home/wardah/dir1/ /home/wardah/dir2
(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:
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
The above command will copy a “dir1” directory to the “dir2” directory.
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.