1

as above

I think it's best explained with an example.

/mnt/source/source-folder
  -> sfolder1
  -> sfolder2 
  -> sfolder3
  -> sfolder4

The "sfolder" contains 10s of folders and 100s of files inside them.

I want to copy "sfolder2 and its contents" to /mnt/destination/destination-folder.

I tried

rsync -nriv --stats --include "sfolder2" --exclude "*" /mnt/source/source-folder/ /mnt/destination/destination-folder/

rsync -nriv --stats --include "sfolder2*" --exclude "*" /mnt/source/source-folder/ /mnt/destination/destination-folder/

rsync -nriv --stats --include "*sfolder2*" --exclude "*" /mnt/source/source-folder/ /mnt/destination/destination-folder/

and in all cases, stats report that only the "sfolder2" folder was transferred and the 10s of folders and 100s of files inside "sfolder2" was ignored.

What am I doing wrong?

My real aim is to copy multiple folders (from my sample folder setup above, sfolder2 and sfolder4) from the source to my destination. I'm only using 1 folder for now while testing.

Thanks!

2 Answers 2

2

You excluded "everything everywhere" with * and included sfolder2 which only transfers a file or directory sfolder2 in the transfer root directory.

Instead you could use /* to only exclude files relative to the source directory. This means if e.g. /sfolder3 is excluded, then all files inside /sfolder3 are also excluded since the parent directory is excluded.

Using /sfolder2/ makes it explicit that the directory is relative to the source directory (/ at start) and is a directory (/ at end). I also replaced -r with -a (archive mode) which includes -r:

rsync -naiv --stats --include='/sfolder2/' --exclude='/*' \
 /mnt/source/source-folder/ /mnt/destination/destination-folder/

To include sfolder2 and sfolder4:

rsync -naiv --stats --include='/sfolder2/' --include='/sfolder4/' --exclude='/*' \
 /mnt/source/source-folder/ /mnt/destination/destination-folder/
1

You can use relative paths, thereby avoiding all the --include and --exclude complications. Here, the --relative (-R) flag tells rsync to maintain the source paths at the destination. The /./ in the source marks the start of the path to be maintained (without it the entire path would be kept)

rsync --dry-run -aivR --stats /mnt/source/source-folder/./sfolder2/ /mnt/destination/destination-folder

This will copy sfolder2 and its contents to /mnt/destination/destination-folder/sfolder2.

To copy multiple folders (sfolder2 and sfolder4) you can use multiple source paths. Like this if your shell handles braced groups {x,y},

rsync --dry-run -aivR --stats /mnt/source/source-folder/./sfolder{2,4}/ /mnt/destination/destination-folder

or this otherwise

rsync --dry-run -aivR --stats /mnt/source/source-folder/./sfolder2/ /mnt/source/source-folder/./other4/ /mnt/destination/destination-folder

(As an aside, rather than using just --recursive (-r) for copying a tree of files, either use --archive (-a) instead or add --times (-t) to help rsync with optimising future copies of the same files. In my example I've used -a; you could use -rt if you want to avoid copying ownerships and permissions.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .