I want to sync all *.sh files in exactly one sub directory. I tried this cmd but all files in a directory are synced instead of only particular file types.

rsync -vr -n --include="*.sh" --exclude="*/*/" --prune-empty-dirs /source /target

I also tried adding a filter like

--filter="+ *.sh"

but this did not change the result. Another filter

rsync -vr -n --include="*.sh" --filter="-! *.sh" --exclude="*/*/" --prune-empty-dirs

gives me an empty list. If I exclude "*" I also exclude "*.sh"... What is wrong? Thanks!

The depth should be one - the name of the subdirectory is not known. For example,


In the above example rsync should start in home, sync directory hierarchy and only the files file1.sh and file2.sh

  • 1
    What do you mean by "in exactly one sub directory"? Do you mean you know what subdirectory the files are in? If so, what's the name or path of that directory?
    – Kusalananda
    Commented Sep 25, 2020 at 10:33
  • 3
    Welcome to the site. Why did you use the -r option if you don't want to descend into all subdirectories (you said "in one subdirectory")? Would rsync -v /source/*.sh /target not do the trick?
    – AdminBee
    Commented Sep 25, 2020 at 11:11
  • You write, "rsync only special file types" and then you say you want to copy *.sh files. The word special has a particular meaning (devices, pipes, etc.). On the other hand, *.sh files aren't special; they just have three characters on the end of the filename. Commented Sep 25, 2020 at 15:53
  • You're right! Sorry for making my first question not clear enough! Note to me: always add an example for clarification... Commented Sep 28, 2020 at 9:31

1 Answer 1


Your examples don't seem to match your description. I think what you are saying you want is this,

  • match all the *.sh files in an unknown subdirectory immediately underneath your /home
  • put the files in the unknown subdirectory on the target
  • do not include /home in the destination path

Looking from /home, this command will match all files */*.sh and copy them and their partial paths to the target. (Remove the --dry-run if you are happy with the intended result.)

rsync --dry-run -avR /home/./*/*.sh /target

For example, /home/subdir/file.sh will be copied to /target/subdir/file.sh

  • 1
    That's it! Can't believe that it's so easy and I got lost with unnecessary --include/--exclude! Thank you very much for pointing me back to the track! Commented Sep 28, 2020 at 9:35
  • @AlexanderFieroch pleased it works for you. The --relative (-R) flag can be really useful in these kind of situations, particularly with its /./ place marker Commented Sep 28, 2020 at 9:45
  • 1
    Yes, I tried -R and /./ with and without - great flags in this situation that simplify a lot. Thanks! Commented Sep 29, 2020 at 10:59

You must log in to answer this question.

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