rsync Command to Exclude a List of Files and Directories in Linux

October 27, 2014 | By
| Reply More

rsync is a very useful and popular linux tool being used for backup and restoring files, but also for comparing and syncing them. We already shown you in the past how to use rsync command in linux with examples and today we will add a few more useful tricks you can use rsync at.

Exclude a list of files and directories

Sometimes when we do large syncs we may wish to exclude a list of files and directories from syncing, in general files that can't by synced like device files and some system files, or files that would take up unnecessary disk space like temporary or cache files.

First let's make a file called "excluded" (you can name it whatever you wish) and write each folder or file we would like to exclude on a separate line like this. For our example if you wish to do a full backup of your root partition you should exclude devices directories that get created at boot time and directories that hold temporary files, your list may look like this:

rsync excluded

Then you can run the following command to backup your system:

$ sudo rsync -aAXhv --exclude-from=excluded / /mnt/backup

rsync exclude file

Exclude files from the command line

You can also exclude files directly from the command line, this is useful when you have a smaller number of files to exclude and you wish to include this in a script or crontab and don't want your script or cron to depend on another file to run successful.

For example if you wish to sync /var to a backup directory but you don't wish to include cache and tmp folder that usualy don't hold important content between restarts you can use the following command:

$ sudo rsync -aAXhv --exclude={"/var/cache","/var/tmp"} /var /home/adrian/var

rsync exclude

This command will be easy to use in any script or cron and will not depend on another file.

Filed Under : LINUX COMMANDS, LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.