If you have a lot of tab characters inside a text document or program file, they can be annoying because of how they're represented differently in various text editors. For example, when you send your code to someone else, is it going to display the same way on their screen as it did on yours? Tabs are a bit unpredictable in this respect, and spaces are a much safer bet.

In this guide, we'll show how to replace all the tab characters inside a file with spaces on Linux. We can use various Linux commands to do the job for us, which we'll go over below. Depending on your situation and the file in question, some commands may be more suitable than others.

In this tutorial you will learn:
  • How to replace tab characters with spaces
Replacing all tab characters with spaces on Linux
Replacing all tab characters with spaces on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software sed, find, perl, expand
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Replace tab characters with spaces

Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.

Check out some of the examples below to decide which command you'd like to run on your file(s).

  1. The following sed command will replace each tab character with five spaces. This uses a native Linux command so that no extra software is required, however you'll need to specify a new name for the file.
    $ sed 's/\t/     /g' tab-file.txt > no-tab-file.txt
    As you can see in the image below, the file with the spaces has much more consistent spacing to the human eye.
  2. Replacing all tab characters with spaces on Linux
    Replacing all tab characters with spaces on Linux
  3. To avoid creating a new file, you can use the -i (in place) option with sed.
    $ sed -i 's/\t/     /g' tab-file.txt
  4. To replace the tab characters in a lot of files, for example all .txt files in a directory, you can use the sed command with a wildcard.
    $ sed -i 's/\t/     /g' *.txt
  5. To run this command recursively, we can use the find command. This example will replace all tabs in .txt files.
    $ find . -type f -name "*.txt" -exec sed -i 's/\t/     /g' {} \;
  6. Note that there are a handful of other command line tools that can also perform the job, such as perl.
    $ perl -p -e 's/\t/     /g' tab-file.txt > no-tab-file.txt
  7. Or take this expand command for example, which will only replace leading tabs on each line, and replace them with four spaces.
    $ expand -i -t 4 tab-file.txt > no-tab-file.txt

Closing Thoughts

In this guide, we saw how to replace all of the tab characters with spaces in a file on Linux. We are able to do this with the native sed command, and recursively with help from find. There are also the perl and expand utilities, which can do the job as well. Like with many things on Linux, there are hundreds of ways to accomplish this simple task. We've shown you some of the easiest and best methods.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
Get extra help by visiting our LINUX FORUM or simply use comments below.