How can I convert tabs to spaces in every file of a directory?

后端 未结 19 1294
既然无缘
既然无缘 2020-12-02 03:48

How can I convert tabs to spaces in every file of a directory (possibly recursively)?

Also, is there a way of setting the number of spaces per tab?

19条回答
  •  天命终不由人
    2020-12-02 03:59

    Warning: This will break your repo.

    This will corrupt binary files, including those under svn, .git! Read the comments before using!

    find . -iname '*.java' -type f -exec sed -i.orig 's/\t/ /g' {} +

    The original file is saved as [filename].orig.

    Replace '*.java' with the file ending of the file type you are looking for. This way you can prevent accidental corruption of binary files.

    Downsides:

    • Will replace tabs everywhere in a file.
    • Will take a long time if you happen to have a 5GB SQL dump in this directory.

提交回复
热议问题