How to remove trailing whitespaces for multiple files?

前端 未结 7 908
长发绾君心
长发绾君心 2020-12-12 14:04

Are there any tools / UNIX single liners which would remove trailing whitespaces for multiple files in-place.

E.g. one that could be used in the con

7条回答
  •  隐瞒了意图╮
    2020-12-12 14:55

    Unlike other solutions which all require GNU sed, this one should work on any Unix system implementing POSIX standard commands.

    find . -type f -name "*.txt" -exec sh -c 'for i;do sed 's/[[:space:]]*$//' "$i">/tmp/.$$ && mv /tmp/.$$ "$i";done' arg0 {} +
    

    Edit: this slightly modified version preserves the files permissions:

    find . -type f -name "*.txt" -exec sh -c 'for i;do sed 's/[[:space:]]*$//' "$i">/tmp/.$$ && cat /tmp/.$$ > "$i";done' arg0 {} +
    

提交回复
热议问题