Copy files and directories in the Linux terminal by using the following command: cp -Rp For example, to copy a file named myfile to the directory mydir, use the following command: cp -Rp myfile mydir


RELATED: How to Copy and Paste Text into the Linux Terminal

Copy Files and Directories in Linux

cp and rsync are two of the most popular commands that you can use to quickly copy files and directories in Linux. We’ll introduce you to both of them.

Using the cp Command

cp stands for copy and is, you guessed it, used to copy files and directories in Linux. You can use cp to copy files to a directory, copy one directory to another, and copy multiple files to a single directory. Here are all examples that demonstrate the use of the cp command.

Consider cp‘s syntax in its simplest form.

An example of its use would look like this.

The above command will copy the text file Test1.txt to the copy_Test1_here/ directory.

If you have multiple files that you need to copy to one directory, simply list each file with a space in between.

You can also copy a file to a directory but save it using a different name. Here’s the syntax for that.

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 :

Want to keep it in the same directory? You can copy a file and rename it like so:

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.

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 .

If you’re unsure if the file or directory was copied to the destination location, you can also use the option -v to print the names of the files or directories that were copied (like a 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.

The output would look like this:

RELATED: 37 Important Linux Commands You Should Know

Using the Rsync Command

The rsync command stands for “Remote Sync” and is primarily used to transfer files and directories between computers on the same network. However, it also allows copying files and directories on the same PC. Here are some examples.

RELATED: How to Use rsync to Backup Your Data on Linux

Considering that we have two directories dir_1 , dir_2 , and a file Test.txt in dir_1 . If we want to copy the file to dir_2 , here’s what the rsync command to do the same would look like.

Here -v is short for “verbose.” It causes the details of the file transfer to appear, similar to the -v option in the cp command. This allows you to confirm the copy was successful. Here’s what the output will be in our case:

You can also copy multiple files at once from one directory to another. Assume that you’re in /Desktop in which there are two directories dir_1 and dir_2 . dir_1 has four files test1 , test2 , test3 , and test4 and you want to copy them to dir_2. Here’s how you can do that using rsync:

The above command will copy the files test1 , test2 , test3 , and test4 to the directory dir_2 .

To copy one directory to another, we must use the -a option. Here’s an example of how to copy directories using Rsync.

If you’re new to Linux and commands seem hard to follow, make sure to take your time and familiarize yourself with basic Linux commands first. You can also learn a lot about commands by using the man command.

Apart from rsync and cp, the install command also allows you to copy files in Linux.

RELATED: How to Copy Files Using the “install” Command on Linux