How can I specify specific files to include/exclude in grsync/rsync?

Currently, I use different operations to backup files from only specified folders. I want to do it now in 1 pass. I think I should be looking at the include/exclude options?

Suppose I have a folder structure like

  files in root here ... 

I want to only backup files/folders from inc1 & inc2 how can I do it?


You can use --include and --exclude options or for a backup process you may want to use --include-from so you can list the folders you want to back up in a file.

For example your file may be called includes.txt and save in your home directory. It would contain


And the rsync command to back up folders inc1 and inc2 in your home directory would be

rsync $HOME --include-from=~/includes.txt /home/backup/

rsync is very flexible, you are best off consulting the man page first and if you get stuck asking a more specific question.

  • When testing rsync use options -nv which will show you what it will do without actually doing it. – Richard Holloway Aug 9 '11 at 12:05

To include only specific directories, include them and their contents, then exclude *. If the directories aren't at the root, you need to include every directory leading to them. For example, to include only /source/inc1, /source/inc2 and /source/also/included:

rsync -a --include='/inc1/***' --include='/inc2/***' \
         --include='/also' --include='/also/included/***' \
         --exclude='*' /source /target

See also a quick guide to common rsync filters.

  • The answer is perfect, though one need to reed carefully. To backup --include='/also/included/***' one does first need to include the parent folder --include='/also'. Forgetting to do so skips '/also/included/***' completely. – Pro Backup Sep 1 '18 at 2:50

Maybe I'm missing something, but you can just specify those folders to rsync:

rsync -Pav /inc1 /inc2 /path/to/backup/folder/

This will create:

  • When you want to use a remote server as the source, I'm guessing you'd have to add user@server: before every folder. I just tried rsync -nav root@server:/etc/psa root@server:/usr/local/psa /localdir/ and it didn't work. So I think this technique only works with local sources. – Chad von Nau Jul 11 '13 at 0:10
  • @ChadvonNau: rsync will work with at most one remote (ie, either a remote source or a remote destination). In your example, you're using both a remote source and a remote destination. – Jeremy Kerr Jul 12 '13 at 7:16
  • I was attempting to do two remote source folders and a local destination. Trying to apply your two source technique to a remote host situation. – Chad von Nau Jul 12 '13 at 20:32
  • 1
    @ChadvonNau: Ah, I didn't interpret the newline as a space there! In that case, try rsync -nav 'root@server:{/etc,/usr/local}/psa' /localdir/ – Jeremy Kerr Jul 23 '13 at 7:40
  • That's awesome. It doesn't maintain the directory structure the way doing it with an includes file would, but it's way easier to setup. – Chad von Nau Jul 24 '13 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.