How to Copy Files and Directories in Linux Terminal
Like any other operating system, you can copy and paste files and directories (folders) into a Linux distribution using the desktop interface. But you can save time by copying these files to the terminal. Here’s how.
RELATED: How to Copy and Paste Text in Linux Terminal
Copy files and directories in Linux
rsync are two of the most popular commands you can use to quickly copy files and directories in Linux. We will introduce you to both.
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 one directory, copy one directory to another, and copy multiple files to a single directory. Here are all the examples that demonstrate the use of the
cpthe syntax of 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/ phone book.
If you have multiple files that you need to copy to a directory, just 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 under a different name. Here is the syntax for it.
cp [file] [directory]/[new filename]
This will copy the contents of the first thumbnail and save it to the directory under the new filename. A concrete example of the same would look like this.
Since there is a file
Test1.txt which must be copied to the directory
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 this:
cp Test1.txt Test2.txt
Want to copy an entire directory? suppose that
dir_2 are two directories in
/Desktop . To copy
dir_2 using the
cp command, here is what you need to type.
cp -a dir_1 dir_2
-a stands for “archive” and is used to let the computer know that we are dealing with directories. The command copies the directory
If you are unsure whether the file or directory was copied to the destination location, you can also use the option
-v to print the names of files or directories that have been copied (like computer program output).
For example, suppose there is a file
Test_Example which should be copied to
dir_1 . To view the output of the copy operation, you would use the
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
rsync The command stands for “Remote Sync” and is mainly 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 back up your data on Linux
Considering that we have two directories
dir_2 and a file
dir_1 . If we want to copy the file to
dir_2 Here is what
rsync command to do the same would look like.
rsync -v dir_1/Test.txt dir_2
-v is short for “verbose”. This brings up the file transfer details, similar to
-v possibility in the
cp order. This allows you to confirm that the copy was successful. Here is 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. Suppose you are in
/Desktop in which there are two directories
dir_1 has four files
test4 and you want to copy them into
dir_2. Here is how you can do it using
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
test4 to the directory
To copy one directory to another, use the
-a option. Here is an example of copying directories using Rsync.
rsync -av dir_1 dir_2
If you are new to Linux and the commands seem difficult to follow, be sure to take your time and familiarize yourself with the basic Linux commands first. You can also learn a lot about commands using the
install The command also lets you copy files in Linux.
RELATED: How to copy files using the “install” command in Linux