DEV Community

tetractius
tetractius

Posted on

TetraQuicky05: The right way of copying recursively a directory - the right flags of rsync

TL;DR

rsync -avru --info=progress2 <Source_Dir>/ <Destination_Dir>/

More on that

I use the above so much that I still don't understand why I didn't set it as alias.

The advantage over the normal cp -rvf is that rsync copies incrementally and it is able to continue if it is interrupted.
It works great also for copying over the network with samba and stuff like that (I use it most of the time over samba drive mounted with cifs and I never had a problem)

There are a gazillion of frags (have fun with man rsync).
Specifically I am always after:

  • -a a cumulative flags that enforce to preserver during the copy also symlinks, permission timestamps and all the things of the files
  • -v just to print what are you copying
  • -r recursively
  • -u important one; update that means that if the is a difference between a file in source and destination the source will be always updates (usefull when a previous run, left some file incomplete and you want to make sure the correct version from the source is overwritten) but if the files are the same, just skip it - that actual make rsync the best incremental tool
  • --info=progress2 is "tries" to give you a progress status percentage of the files while you are copying (where there is a lot of stuff it might be wrong on the road, but hey... better than nothing right?)

you will not use cp -rvf any more after this post (also because rsync is basically included in every distro on planet).


NOTE:
A remember to put the slash at the end of the directories you want to copy if you don't want rsync to put a new directory under you destination directory. Otherwise if you don't do this...

rsync -avru from/   to/

you will end up with:

to/from/

in the destination.


...and for Powershell?

I've read about robocopy but it seems something bizantine at first touch. Personally in windows I still usersync via WSL. I used to use it also via cygwin although it was really slow (and the cygwin version was super out-of-date there there was not --info=progress2 flags)

Bonus

You need to sync and mirror 2 directories (like... also deleted files from source to be deleted in destination)? Have a look at --delete flags. Slow copy over the network? Give a try with -z (although sometime compressing big file might waste more of your time if you computer is not like super powerful).

Top comments (0)