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?

1 Answer 1

3

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.

4
  • 1
    Does this enter in hidden files/directories? Jan 1, 2014 at 11:45
  • 1
    @Johnツ Yes, it does. If you don't want that, you can use -not -path '*/\.*'.
    – Chris Down
    Jan 1, 2014 at 11:53
  • How can I ignore binary files (pem, image files, compiled files etc)? Apr 11, 2014 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, 2014 at 6:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .