How to run this `find` command, but only on non-binary files

findunix

I want to remove trailing whitespace from all the files in a recursive directory hierarchy. I use this:

find * -type f -exec sed 's/[ \t]*$//' -i {} \;

This works, but will also remove trailing "whitespace" from binary files that are found, which is undesirable.

How do I tell find to avoid running this command on binary files?

Best Answer

You could try to use the Unix file command to help identify the files you don't want, but I think it may be better if you explicitly specify what files you want to hit rather than those you don't.

find * -type f \( -name \*.java -o -name \*.c -o -name \*.sql \) -exec sed 's/[ \t]*$//' -i {} \;

to avoid traversing into source control files you might want something like

find * \! \( -name .svn -prune \) -type f \( -name \*.java -o -name \*.c -o -name \*.sql \) -exec sed 's/[ \t]*$//' -i {} \;

You may or may not need some of the backslashes depending on your shell.

Related Question