Key Takeaways
The cp command can be used to copy files to a directory, copy multiple files to a single directory, and copy a file to a directory with a different name.
The rsync command is not only useful for transferring files between computers but it also allows for copying files and directories on the same PC. In addition, it provides a comprehensive overview of the file transfer process. While Linux distributions typically offer a desktop interface for copying and pasting files and directories, using the Terminal to copy these files can be more efficient. Here is how you can do it.
What is the cp Command?
The cp command, which stands for copy, is a built-in tool in Linux used for copying files and directories. It allows you to copy files to a specific directory, copy one directory to another, and copy multiple files to a single directory. Below are several examples showcasing the usage of the cp command.
Copy File on Linux with the cp Command
Consider cp's syntax in its simplest form.
cp [file] [directory]
An example of its use would look like this.
cp Test1.txt copy_Test1_here/
The command mentioned above will duplicate the text file Test1.txt to the directory named copy_Test1_here/.
To copy several files to a single directory, just list each file separated by a space.
cp Test1.txt Test2.txt Test3.txt Test4.txt copy_all_Tests_here/
You can also copy a file to a directory but save it using a different name. Here's the syntax for that.
cp [file] [directory]/[new filename]
This will copy the contents of the first tile and save it in the directory as the new filename. A real-life example of the same would look like this.
Given that there's a file Test1.txt that needs to be copied to the directory copy_Test1_here as Test2.txt :
cp Test1.txt copy_Test1_here/Test2.txt
Want to keep it in the same directory? You can copy a file and rename it like so:
Copy Test1.txt to Test2.txt. If you want to verify the file's destination, you can also include the -v option to display the names of the copied files or directories (similar to computer program output).
For example, let's assume that there's a file Test_Example that needs to be copied to dir_1 . To display the output of the copy operation, you'd use the -v option.
cp -v Test_Example.txt dir_1
The output would look like this:
'Test_Example.txt' -> 'dir_1/Test_Example.txt
Copy a Directory on Linux
Want to copy an entire directory? Let's assume that dir_1 and dir_2 are two directories in /Desktop . To copy dir_1 to dir_2 using the cp command, here's what you need to type.
cp -a dir_1 dir_2
Here option -a stands for "archive" and is used to let the computer know that we're dealing with directories. The command copies the directory dir_1 to dir_2 .
Most of the same rules that apply to copying files also apply to copying directories. For instance, you can copy a nested directory in the same way as you would copy a nested file. Additionally, you can use the -v argument with the main cp command to display the copy operation that has been performed.
The -a flag is a combination of two other flags, namely -R and -d. The -R argument indicates that the copy operation should be recursive, meaning that it will copy both the contents of the object being copied and the object itself. This is crucial when copying folders. The -d argument ensures that all file and folder attributes are preserved.
It is possible to copy folders with just cp -r dir_1 dir_2 but you may lose specialized attributes if you do that.
Copy Files and Directories with rsync
The rsync command, short for "Remote Sync," is primarily employed for transferring files and directories between computers within the same network. Nonetheless, it also enables the copying of files and directories on the same PC. Here are a few examples.
Suppose we have two directories, dir_1 and dir_2, along with a file named Test.txt within dir_1. To copy the file into dir_2, the following is the rsync command that accomplishes the task.
rsync -v dir_1/Test.txt dir_2
In this case, the -v option is used as an abbreviation for "verbose," which displays the file transfer details similar to the -v option in the cp command. This feature enables you to verify the successful completion of the copy process. The output for our scenario would be as follows:
sent 110 bytes
received 35 bytes
290.00 bytes/sec
Total size is 24
Speedup is 0.17
You can also copy multiple files at once from one directory to another. Let's say you are currently in the /Desktop directory, which contains two directories: dir_1 and dir_2. In dir_1, there are four files named test1, test2, test3, and test4 that you want to copy to dir_2. To achieve this, you can use the rsync command with the following paths:
rsync -v dir_1/Test1.txt dir_1/Test2.txt dir_1/Test3.txt dir_1/Test4.txt dir_2
The command mentioned above will copy the files test1, test2, test3, and test4 to the directory dir_2.
To copy a directory to another, the -a option must be utilized. An example of copying directories using Rsync is provided below.
rsync -av dir_1 dir_2
If you find Linux commands difficult to understand, it's important to spend some time acquainting yourself with the basics. Utilizing the man command can also provide a wealth of knowledge about different commands.
In addition to rsync and cp, the install command offers the capability to duplicate files within Linux.