How to remove trailing whitespace of all files recursively?
Here is an OS X >= 10.6 Snow Leopard solution.
It Ignores .git and .svn folders and their contents. Also it won't leave a backup file.
export LC_CTYPE=Cexport LANG=Cfind . -not \( -name .svn -prune -o -name .git -prune \) -type f -print0 | perl -0ne 'print if -T' | xargs -0 sed -Ei 's/[[:blank:]]+$//'
Use:
find . -type f -print0 | xargs -0 perl -pi.bak -e 's/ +$//'
if you don't want the ".bak" files generated:
find . -type f -print0 | xargs -0 perl -pi -e 's/ +$//'
as a zsh user, you can omit the call to find, and instead use:
perl -pi -e 's/ +$//' **/*
Note: To prevent destroying .git
directory, try adding: -not -iwholename '*.git*'
.
Two alternative approaches which also work with DOS newlines (CR/LF) and do a pretty good job at avoiding binary files:
Generic solution which checks that the MIME type starts with text/
:
while IFS= read -r -d '' -u 9do if [[ "$(file -bs --mime-type -- "$REPLY")" = text/* ]] then sed -i 's/[ \t]\+\(\r\?\)$/\1/' -- "$REPLY" else echo "Skipping $REPLY" >&2 fidone 9< <(find . -type f -print0)
Git repository-specific solution by Mat which uses the -I
option of git grep
to skip files which Git considers to be binary:
git grep -I --name-only -z -e '' | xargs -0 sed -i 's/[ \t]\+\(\r\?\)$/\1/'