2
rsync -avzm -e ssh /path/to/src/folder/ /path/to/dest/folder 
 --include=*/ --include=*/config.xml --exclude=*

I need to copy files(say config.xml) that are in the 2nd level directories using rsync. I am currently using the above script and its copying config.xml's from all directory levels.

What changes should I make in the above command?

2

Try this:

rsync -avzm -e ssh -f'- /*/*/' -f'+ config.xml' \
    /path/to/src/folder/ /path/to/dest/folder

Explanation

  • -f causes rsync to enable filter rule.
  • - /*/*/:
    • - meaning exclude rule.
    • /*/*/ exlucde any subdirectory from 3rd level.
  • + config.xml: + meaning include rule, cause rsync copy all files that have config.xml in filename.

Example

$ ls -R dir1/
dir1/:
dir2  dir22

dir1/dir2:
config.xml.2  dir3

dir1/dir2/dir3:
config.xml

dir1/dir22:
config.xml.22  dir33

dir1/dir22/dir33:
config.xml

$ rsync -r -f'- /*/*/' -f'+ config.xml' dir1/ /tmp

$ ls -R /tmp/dir2*
/tmp/dir2:
config.xml.2

/tmp/dir22:
config.xml.22
  • Congrats on getting to 10k! – slm Jun 7 '14 at 2:27

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.