Convert Tabs to Spaces on Linux
Practical Linux commands for replacing tab characters with spaces in text files using expand, sed, awk, or tr.
Before you start
Tab conversion runs locally with standard command-line tools already included in most Linux distributions. You need a terminal and read access to the file being converted. The main tool, expand, is part of the coreutils package. Check whether it is available by running: command -v expand. If this returns a path, you can continue. The process works on text files; binary files should not be converted this way.
Step-by-step instructions
First create a backup copy of the file in case the output needs adjustment: cp input.txt input.txt.bak. Replace input.txt with the actual file name.
Use expand to convert tabs into spaces and write the result to a new file: expand input.txt > output.txt. By default, expand replaces each tab with the number of spaces needed to reach the next tab stop. Default tab stops are placed every eight columns.
To use a different tab width, set an explicit tab stop list. For four-column spacing: expand -t 4 input.txt > output.txt. The -t option accepts a single number as a repeating interval or a comma-separated list of column positions such as 4,8,12.
To avoid creating a separate destination file and keep the result under the original name, you can write to a temporary file and then move it: expand -t 4 input.txt > input.txt.tmp && mv input.txt.tmp input.txt. Make sure the command exits successfully before the file is replaced.
For simple cases where every tab should become the same fixed number of spaces, tr can be used: tr '\t' ' ' < input.txt > output.txt. This does not align text to tab stops; it replaces each tab character with one space by default. To use a fixed number such as three spaces per tab, combine tr with sed: tr '\t' '\n' < input.txt | sed 's/^/ /' is not equivalent, so prefer a method that explicitly replaces each tab where that behavior is required.
When tabs appear only at the start of lines and indentation needs to be adjusted, use sed: sed 's/^\t/ /' input.txt > output.txt. This replaces each leading tab individually with a chosen indentation string. To apply it to multiple leading tabs, repeat the substitution or use a loop form that matches the desired number of levels.
Check the output
Inspect the result with a viewer that can reveal tab characters. The command cat -vet output.txt displays tabs as ^I and marks line endings, making it easy to confirm that no tab characters remain in the converted file.
For a count-based check, use: grep -c $'\t' output.txt. If the output is zero, the file contains no tabs. Keep in mind that expand interprets tab stops using column positions, so the resulting spacing can vary when text precedes a tab.
Common problems and next steps
A common concern is column alignment changing after conversion. Because tabs move text to the next configured tab stop, replacing them with spaces can shift content if the original file used a different tab width than the one chosen in the command. Re-run expand with a matching -t value and compare the backup.
Very large files may take noticeable time to process. If this is an issue, check that you are not processing a binary file by mistake. The file command can help identify unusual file types before conversion.
After the output looks correct, you can remove the temporary copy once it is no longer needed. Review the file with cat -vet and verify line counts before discarding the backup.