How to remove trailing whitespace of all files recursively?

前端 未结 15 1967
难免孤独
难免孤独 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:57

    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'
    

提交回复
热议问题