Copying Directories with "rsync"

man using laptop
Ezra Bailey/Taxi/Getty Images

If you aren't familiar with the rsync command yet, it may be worth while to give it a spin. While the name and the power of rsync can be intimidating at first, getting comfortable with it can be quite easy if you consider it as just another file copy command that gives you a number of additional options.

One of the useful features of rsync is that when you use it copy directories, you can exclude files in a systematic way.

The basic syntax is just like that of rcp:

rsync source target

For example

rsync /data/*.doc comp1:/home/jk/docs/

copies all files with extension doc to the directory /home/jk/docs/ on the computer comp1.

To copy directories you use the "-r" (recursive) option as usual:

rsync -r /data comp1:/home/jk/docs/

To exclude files you can use the --exclude option:

rsync -r --exclude="*.jpg" /data comp1:/home/jk/docs/

To exclude files from the excluded list you use the "--include" option:

rsync -r --exclude="*.jpg" --include="*keep*" /data comp1:/home/jk/docs

This comes in handy for example when you want to copy a directory that contains large files that you don't really care about. It retains the directory structure but gives you the option to leave out files that would just waste disk space.