2

I need to replace all unwanted spaces in all files from the current directory and from directories from the current directory (recursive search).

I define the unwanted spaces the spaces and tabs that are at the end of the line and are not followed by any other character than \n (the new line character).

e.g.:

This is a line with unwanted spaces    
        // this line is supposed to contain only spaces
Another line without unwanted spaces
Another line with unwanted spaces     

This snippet will become:

This is a line with unwanted spaces

Another line without unwanted spaces
Another line with unwanted spaces

How can I remove all unwanted spaces from all files (recursive search) from the current directory?

2

Something like the following should suit your needs:

find -type f -exec sed -i 's/[[:blank:]]\+$//' {} \;

Note that sed's -i option is not safe with symlinks -- it will break them. If this is a problem, consider using ed or ex.

  • Does this enter in hidden files/directories? – Ionică Bizău Jan 1 '14 at 11:45
  • @Johnツ Yes, it does. If you don't want that, you can use -not -path '*/\.*'. – Chris Down Jan 1 '14 at 11:53
  • How can I ignore binary files (pem, image files, compiled files etc)? – Ionică Bizău Apr 11 '14 at 6:07
  • pem is not a binary file. Either way, the better way is to include what you actually need -- use -name, -iname, or similar. – Chris Down Apr 11 '14 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.