Pre-processing data is very important for maintaining statistical analysis accurate and reliable. But there is no doubt that some data sets are very selective and need cleanup. If you find yourself working with an ungodly amount of spaces within your datasets file, the unexpand command in Linux might help you automate the cleanup.
What is unexpand Command in Linux?
The unexpand
command takes input from files or content via standard input. It will look for space and convert them into tabs. When no output location is specified, the results will be displayed in the standard output.
Although unexpand
’s functionality doesn’t make sense at first glance, it is a great command for making text navigation and editing easier. It also cleans up unnecessary spaces to make the information more cohesive without sacrificing the visual aspects when viewing the file.
Imagine if you need to edit the content on the far right side of the screen. Spamming with your right arrow key through tabs is faster than spamming through blank space characters. It can also help identify single accidental spaces at the beginning.
Users would need to use the cat
command with -A
option to see what is happening when utilizing the unexpand command. This can be seen in the example below, the sample file printed above consists of spaces.
Once the unexpand is applied, there are “^I
” characters that appear where the space used to be. The “^I
” character indicates tabs, which means that all spaces between the numbers and words are successfully converted into tabs. $
signs only indicate the end of the line for each sentence.

In the second prompt, the user executed the unexpand
command with -a
on it, meaning all spaces are and then turned into tabs. The results for processing the “sample” file are funneled into a file called ”edited.”

Note: The output in both sample and edited files is not noticeable in the standard output, so you need to use -A
to see the difference. Another option is to open the file to confirm if the space is converted to tabs.
Default unexpand Syntax
The unexpand
command has four parts by default: the command itself, additional options, and the input or output file. By default, unexpand
will show the processed file at the standard output. But if you add the fourth part, it will create a file to store the result of its processes.
$ unexpand option inputfile > outputfile
Interestingly, this is also the same default format for unexpand
’s close relative, expand
. But instead of converting spaces into tabs, expand
converts tabs with spaces. More information about the expand
command is down below.
Once you understand unexpand
and its syntaxes, the expand
command would also be easy to use.
Relevant Options for unexpand Command in Linux
There are only 3 relevant options for the unexpand
command: -a
, -t
, and --first-only
. You can also use the --help
and --version
options if you find this information helpful. But all in all, knowing how to use -a
, -t
, and --first-only
gives you a functional grasp of the whole command itself.
Using -a or –all
Adding the -a
or --all
option is necessary if you want to convert all the instances of spaces to tabs. By default, unexpand
will only convert the initial spaces and will not touch irrelevant blanks.
However, if you want to be consistent with the result, the -a
option is for you. Just note that you might accidentally convert instances where you want spaces to stay unchanged. Funneling the result into a new file is also a good idea.
$ unexpand -a inputfile outputfile
Note: Unexpand
also has the --first-only
option in which only the leading sequences of blanks are overwritten. In this situation, the -a
option is automatically overridden by the --first-only
even if is used alongside the command.
Using -t or –tabs=N
Another option that unexpand
can do is customize the number of default tab characters. Tabs usually consist of 8 characters by default, but you can change that and have a wider or shorter one by using the -t
or --tab
s option.
To use the -t
option, you need to decide on the increase or decrease of tab length that you need for your text files. This is done by adding the preferred number after the -t
option without any spaces.
Effectively, the -t
option becomes -tN
, wherein N
is the number of characters that consists of a new tab. There is also a -t
variation or --tabs=LIST
, which uses commas to indicate the preferred tab distances for the separation of content lists.
$ unexpand -tN inputfile outputfile
Below, we doubled the characters from 8 to 16 to make a wider version of the default tab. After that, the changed content is saved to the wide_tab file for demonstration. As seen in the screenshot below, wide_tab seems like two tabs combined even though it only uses one tab character.
$ unexpand -t16 sample > wide_tab

These changes might not be reflected if you display them in the standard output. But if you open the files using a text editor, the change will be apparent.
What About expand?
expand
is the alternate option for unexpand because it does the opposite. Instead of converting spaces to tabs, the expand
command will turn tabs into spaces. This command is also used for pre-processing texts and is often used prior to content sortation.
Almost all the unexpand options are also available for the expand command, including -t
, -tN
, and the usual --help
or --version
options. It has a unique option -i
, which prevents converting tabs after non-blank characters. When this option is applied, only the tabs preceding the lines will be converted to spaces.
And that’s all you need to know for the unexpand
command. We also lightly scratched the surface of the expand
command and how it differs from unexpand
fundamentally. You can also use our guides on a variety of commands such as atrm
, useradd
, sed
, echo
, and more.
If this guide helped you, please share it. ?