rsync

 Rsync is a useful utility to compare two folders and copy the differences. It works over ssh as well as on local disks, and it can also copy changes to files rather than whole changes. If you use it to copy one folder to another, it can also carry on where it leaves off, if you interrupt it. It is therefore better than other copy utilities.

The general syntax for comparing folder A to folder B and copying the differences is:

rsync -avu A/ B/ --progress

If folder B is on a server, let's say, www.mywebsite.com, then, 

rsync -avu A/ username@www.mywebsite.com:/var/www/B/ --progress --rsh="ssh -p22"

Assuming ssh answers on port 22 and your folder B is located in /var/www/

Note that it is imperative that you put a forwardslash (/) after both folder names. If you don't do that, it will copy one folder into the other.

Note that you can also delete files that are not in the target. This is dangerous.

Say you have folder A and B, and say folder A has items 1,2,3,4, and folder B has items 2,3,4,5.

typing

rsync -avu A/ B/ --progress --delete-excluded

will delete item 5 and copy item 1, because 1 is not in B and 5 is not in A.

I generally advise against this, but if you need the target folder to be identical and you are sure you do not need the surplus items, go ahead.

You can also exclude items.

Say you have folder A and it has items 1,2,3,4, and folder B, with items 1,2. Let's say you want to copy items 3,4 and ignore item 1.

rsync -avu A/ B/ --progress --exclude="1"

Be warned, if you combine --delete-excluded and --exclude, then delete-excluded will delete the items excluded. Never combine these two unless you're certain.

The flags -avu are all generally needed unless your target disk is from a foreign operating system. The -u flag updates the target files with the contents of the source files, but only if the source files are newer. It's short for "update". v is verbose. The flag "a" is for archive, meaning preserve everything including file privileges. Now, windows drives do not understand unix privilege files, so lots of errors are generated if you use -a with a windows drive. Hence, with windows, you use -rulv instead of -avu. The order of the flags doesn't matter, you can use -rul or -rlu or whatever.

  

Popular posts from this blog

Pause a program which is using too much CPU

throttle traffic on apache

/var/log/journal taking up lots of space