I’ve stared working on a project on github, written in php, that have mixed spaces and tabs for indent, I’m using GNU/Linux so I thought that I just replace all tabs by spaces using bash. Here is the command I’ve used that just did that:
find . -type f -name '*.php' | while read file; do grep $'\t' $file > /dev/null && sed -ie 's/\t/ /g' $file done
Explanation:
find . -type f -name '*.php'
– this will search for all files with php extension, in current directory. I’ve used-type f
to return only files since there where directories that end with php.while read file; do ... done
– while loop over files found by find, the filename will be in file variablegrep $'\t' $file > /dev/null
– this will return true if file contain tabs& &
execute next command if previous is truesed -ie 's/\t/ /g' $file
– replace tabs by spaces in file inline (-i option)