Search code examples
bashshellunixspacesin-place

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


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?


Solution

  • 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.