DEV Community

wolfiton
wolfiton

Posted on • Edited on

Difference between cp and rsync

Hi everyone,

Today I wanted to write about the differences between cp and rsync.

Section 1

cp

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:

cd ~

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 /home/dan/Documents.

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 pwd

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 ~/work

~ - 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 bk directory(folder).

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: man cp

Section 2

rsync

The rsync command is a very fast and versatile copy utility that can :

  • create backups

  • copy files and documents between hosts(other computers)

  • many more

It can be defined as the improved copy command that got superpowers and can copy faster and more in very little time.

The 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/Documents and want to move it to /home/dan/bk

But this time we already have the /home/dan/bk folder and have only one new file in our work folder.

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

-anv flags.

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:

  • user permissions

  • groups

  • metadata

  • 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 rsync operation.

Update: Important

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.

Credits:

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

Top comments (6)

Collapse
 
cescquintero profile image
Francisco Quintero 🇨🇴

The rsync one is very useful when moving lots and large files between servers as it can resume downloads.

Collapse
 
wolfiton profile image
wolfiton • Edited

I agree that rsync is useful for the case you mentioned and others as well.

I think that a good summary for rsync is that if you have large files to copy or want to make backups then rsync is your friend.

Thanks for sharing @francisoquintero your opinion and offer an example of a good use case for rsync.

Collapse
 
dhandspikerwade profile image
Devin Handspiker-Wade

Small mistake; you refer to the -a flag as the "the append flag" but it's the called the "archive" flag.

Collapse
 
wolfiton profile image
wolfiton • Edited

Thanks @devin,

I will correct it right away

Collapse
 
krampusnuggets profile image
Brandon Williams

Also, a nice feature that rsync has over cp is displaying the progress using the: --progress flag

Collapse
 
wolfiton profile image
wolfiton

Thanks @brandonwiliams for reminding me of that.