Open In App

expand Command in LINUX with examples

Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you need to do this for many files? For a situation like this, LINUX has a command line utility called expand which allows you to convert tabs into spaces in a file and when no file is specified it reads from standard input. 

Thus, expand is useful for pre-processing character files like before sorting that contain tab characters. expand actually writes the produced output to standard output with tab characters expanded to space characters. In this, backspace characters are preserved in the output and also decrease the column count for tab calculations. 

Syntax of `expand` command in Linux: 

expand [OPTION] FILE

The syntax of this is quite simple to understand. It just requires a file name FILE in which you want to expand tab characters into space characters and it reads from standard input if no file name is passed and gives result to standard output. 

Suppose you have a file name kt.txt containing tab characters. You can use expand as: 

expand kt.txt

Expand will produce the content of the file in output with only tabs changed to spaces.

Note, if there is a need to make this type of change in multiple files then you just have to pass all file names in input and tabs will get converted into spaces. 

You can also transfer the output of the changes made into some other file like: 

expand kt.txt > dv.txt 

Now the output will be transferred to dv.txt as redirection operator > is used.

Options for expand command:

1. `-i, – – initial` option in expand command in Linux

There can be a need to convert tabs that precede lines and leave unchanged those that appear after non-blanks. In simple words this option allows no conversion of tabs after non-blanks. 

//using -i option//

expand -i kt.txt

this will not change those tabs that appear after blanks

2. -t, – – tabs=N option in expand command in Linux

By default, expand converts tabs into the corresponding number of spaces. But it is possible to tweak the number of spaces using the -t command line option. This option requires you to enter the new number of spaces(N) you want the tabs to get converted. 

//using -t option//

expand -t1 kt.txt > dv.txt

This will convert the tabs in kt.txt to 1 space instead of default 8 spaces

You can also use it as: 

expand --tabs=1 kt.tx > dv.txt

This will also convert tabs to one space each

3. -t, – -tabs=LIST option in expand command in Linux: This uses comma separated LIST of tab positions. 

4. – -help option in expand command in Linux: This will display a help message and exit. 

5. –version option in expand command in Linux: This will display version information and exit. 

The number of options is not much when it comes to expanding command. So, that’s pretty much everything about expand command. 


In this article we have discussed the ‘expand’ command in Linux proves to be an efficient solution for converting tabs to spaces in files, offering various options to cater to different scenarios. Its ability to handle large files and support batch processing makes it a valuable tool for users working with text-based data in the Linux environment.

Article Tags :