views:

2318

answers:

4

How can you remove all of the trailing whitespace of an entire project? Starting at a root directory, and removing the trailing whitespace from all files in all folders.

Also, I want to to be able to modify the file directly, and not just print everything to stdout.

+6  A: 

In Bash:

find dir -type f -exec sed -i 's/ *$//' '{}' ';'

(Edit: doesn't give errors for directories; also now only removes trailing whitespace (previously it was removing leading and trailing whitespace))

Adam Rosenfield
This generates errors like this for every file found. sed: 1: "dir/file.txt": command a expects \ followed by text
iamjwc
Replacing ';' with \; should work. (Also quotes around {} are not strictly needed).
agnul
Still getting the same error. :-\
iamjwc
To remove all whitespace not just spaces you should replace the space character with [:space:] in your sed regular expression.
WMR
Another side note: This only works with sed versions >= 4, smaller versions do not support in place editing.
WMR
This is a faster and safer variant:find dir -type f -print0 | xargs -r0 sed -i 's/ *$//'
pixelbeat
+3  A: 

Use:

find . -print0 |xargs -0 perl -pi.bak 's/ +$//'

if you don't want the ".bak" files generated:

find . -print0 |xargs -0 perl -pi 's/ +$//'

as a zsh user, you can omit the call to find, and instead use:

perl -pi 's/ +$//' **/*
Sec
+4  A: 

This worked for me in OSX 10.5 Leopard, which does not use GNU sed or xargs.

find dir -type f -print0 | xargs -0 sed -i .bak -E "s/[[:space:]]*$//"

Just be careful with this if you have files that need to be excluded (I did)!

You can use -prune to ignore certain directories or files. For Python files in a git repository, you could use something like:

find dir -not -path '.git' -iname '*.py'
pojo
Any chance you could clarify this? I'd like a command that will remove trailing whitespace from all files in a directory recursively, while ignoring the ".git" directory. I can't quite follow your example...
trevorturk
A: 

I ended up not using find and not creating backup files.

sed -i '' 's/[[:space:]]*$//g' **/*.*

Depending on the depth of the file tree, this (shorter version) may be sufficient for your needs.

NOTE this also takes binary files, for instance.

Jesper Rønn-Jensen