Situatie
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.
Solutie
The cp command is the native copying tool built into Linux. 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
If you’re unsure if the file 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
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 apply to copying directoreis that apply to files. For example, you can copy a nested directory just like you might a nested file, and you can pass the -v argument to main cp display the copy operation that has been performed.
The -a flag is actually a combination of two other flags: -R and -d. The -R argument specifies that the copy operation is recursive, and that it must copy the contents of the object being copied as well as the object itself — this is essential for 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 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.
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/sectotal 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.
Leave A Comment?