Today I wanted to write about the differences between cp and
The cp command can be translated as the copy command and is integrated by default in Linux.
This command can be used when you just need a simple copy operation for a file or a folder.
The basic syntax looks like this
cp /file_location(where the file or the documents is currently located) /file_destination(where you want your copy to go)
To make things a little clearer let's add a simple example:
pwd - should return
/home/your_username for me it is /home/dan
We are going to use my home location from now on to create our example
So let's say I want to copy my work folder that is located in
/home/dan to the Documents folder location that is
Also, I don't have any files in my work folder.
Before we continue I would like to remind us that we are currently in the /home/dan by using the
cd ~and verifying by
To do this we type:
cp ./work ~/Documents
We use the dot to say that we want the file from the current directory we are in this situation this is equivalent(the same as) to
~ - is equal to typing
/home/user_name(in our case the logged-in user is dan)
This command will make our copy, but if we have files in the
work directory we will need to specify the recursive option(copy everything that is also in the folder).
Going forward we get a lot of documents that need storing and also get a couple of files that go in our work directory.
We decide it's time to copy our files to a backup location, on home/dan/bk folder
So we type the following
cp ~/Documents/work ~/bk
Press enter and we get an error
cp: -r not specified; omitting directory './work'
As we discussed above the
cp command is telling us to use the
-r option if we want to copy the
work directory(folder) to
if we do that our command should look like this
cp -r ~/Documents/work ~/bk
Also to find more about
cp we can always use the man command:
rsync command is a very fast and versatile copy utility that can :
copy files and documents between hosts(other computers)
It can be defined as the improved copy command that got superpowers and can copy faster and more in very little time.
rsync command is great when you have a lot of things to copy and it is smart enough to detect if a certain file has been copied already and skips it.
Let's see how can
rsync help us if we wanted to create a simple backup for our work folder.
As in the example above we have a work folder with files that we just copied to
~/home/dan/Documentsand want to move it to /home/dan/bk
But this time we already have the
/home/dan/bkfolder and have only one new file in our
To use the rsync we type rsync - option flag /file_location /file_destination
So for our example, we can try to test first before making the copy operation using
rsync -anv ./work ~/bk
The output will look like this
rsync -anv work ~/bk ✔ at 15:26:09 sending incremental file listworkwork/file.txt work/file2.txtsent 118 bytes received 26 bytes 288.00 bytes/sectotal size is 0 speedup is 0.00 (DRY RUN)
to do the operation for real(execute the command not just test it) we type:
rsync -av ./work ~/bk
-a - the archive flag will keep every information from the copy location(where the file or folder is currently) to the target location(to where it will be copied)
The information that is copied can be:
so on ...
Thanks @devin for correcting me regarding the -a flag. -a comes from arhive not append.
-v - verbose mode or more info mode will provide more feedback on what is happening during a
Thanks @brandonwiliams for reminding me about the progress flag that will show live progress of the operations.
The --progress flag is used after the other flags like this:
rsync -av --progress ./work ~/bk
I hoped you liked my article Dear Reader.
Also if you found the article useful share it on social.
@devin correction regarding -a flag in
rsync stands(means) for archive not append.
@brandonwiliams for reminding me about the progress flag that will show a live progress of the operations.