Saved for later

How to rsync only one type of files by extension

Having a file structure full of various file types you want to sync only files of one type into a new location.

rsync -rv --include '*/' --include '*.js' --exclude '*' --prune-empty-dirs Source/ Target/

This will generate the same structure found in Source into Target but only including the JavaScript(.js) files.

Note the usage of ' around the arguments containing * since we don’t want it to be expanded in a bash shell.

The first --include '*/' is to make sure sub-directories are scanned. This would also include all directories does not include the file you want resulting in empty directories in Target. To remove these empty directories we use --prune-empty-dirs

The --include '*.js' is rather self explanatory, and you can add more as you need.

Finally we exclude all other files we don’t want using --exclude '*'