Search code examples
linuxrsync

How to exclude binaries when using rsync


I want to rsync a directory to server from a mac machine to linux machine while excluding compiled files like .o files and binary executables. How do I exclude binary files?

What I am using at the moment:

rsync -av --compress --exclude="*.o" dir server:dir

Solution

  • This is a sticky problem because a Unix system does not have a hard and fast definition of the distinction between "binary" and "text" files. You can do a pretty good job by using the file command and searching for text in the output (see How to tell binary from text files in linux), so I'd run find to generate a list of files which file considers to be text, and use that as the list of files to rsync:

    find dir | xargs file | awk -F: '$2 ~ /text/ { print $1 }' | \
        rsync --files-from=- -av --compress dir server:dir
    

    This will require some tweaking to make sure the pathnames are correct relative to the source dir, and so on, but it should get close to what you want.

    In the long term, I'd want to rework my build process to put generated files in a dir/build directory, but this might help for now :-)