Wednesday, September 30, 2015

Rsync tricks and how to rsync only one type of files by extension

Rsync is a wonderful tool. It has many options.

But to sync only a file type among many files in a dir eluded me.

After some googling I found this command:

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

I found it on this blog: https://silentorbit.com/notes/2013/08/rsync-by-extension/

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 '*'


Thank you very much!