npressfetimg-221.png

How to Copy Files and Directories in the Linux Terminal – How-To Geek


fatmawati achmad zaenuri/Shutterstock.com

Like any other operating system, you can copy and paste files and directories (folders) in a Linux distribution using the desktop interface. But you can save time by copying those files in the Terminal. Here’s how.

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.

cp [file] [directory]

An example of its use would look like this.

cp Test1.txt copy_Test1_here/

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.

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:

cp Test1.txt Test2.txt

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 .

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.

cp -v Test_Example.txt dir_1

The output would look like this:

'Test_Example.txt' -> 'dir_1/Test_Example.txt

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.

rsync -v dir_1/Test.txt dir_2

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:

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. 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:

rsync -v dir_1/Test1.txt dir_1/Test2.txt dir_1/Test3.txt dir_1/Test4.txt dir_2

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.

rsync -av dir_1 dir_2

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

Source: https://www.howtogeek.com/778790/how-to-copy-files-and-directories-in-linux-terminal/

Related Posts