How to remove trailing whitespace of all files recursively?

前端 未结 15 1958
难免孤独
难免孤独 2020-12-07 06:58

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.

15条回答
  •  粉色の甜心
    2020-12-07 07:53

    I ended up running this, which is a mix between pojo and adams version.

    It will clean both trailing whitespace, and also another form of trailing whitespace, the carriage return:

    find . -not \( -name .svn -prune -o -name .git -prune \) -type f \
      -exec sed -i 's/[:space:]+$//' \{} \;  \
      -exec sed -i 's/\r\n$/\n/' \{} \;
    

    It won't touch the .git folder if there is one.

    Edit: Made it a bit safer after the comment, not allowing to take files with ".git" or ".svn" in it. But beware, it will touch binary files if you've got some. Use -iname "*.py" -or -iname "*.php" after -type f if you only want it to touch e.g. .py and .php-files.

    Update 2: It now replaces all kinds of spaces at end of line (which means tabs as well)

提交回复
热议问题