Home Linux Commands Expand And Unexpand Commands Tutorial With Examples

Expand And Unexpand Commands Tutorial With Examples

By sk
Published: Updated: 1.4K views

In this guide, we will discuss about two lesser known Linux commands namely Expand and Unexpand with practical examples. For those wondering, the Expand command is used to replace TAB characters with SPACE characters in files and the Unexpand command does the opposite i.e replaces SPACE characters with TAB characters in a file. In fact, MS-DOS also has a command called "Expand", which is used to expand a compressed file. But the Linux Expand command simply converts the tabs to spaces. These two commands are part of GNU coreutils and written by David MacKenzie.

For the demonstration purpose, I will be using a text file named "ostechnix.txt" throughout this guide.

Expand command examples

As stated already, the Expand command replaces TAB characters in a file with SPACE characters.

To convert tabs to spaces in a file, for example "ostechnix.txt", and write the result to standard output using command:

$ expand ostechnix.txt

If you don't want to display the result in standard output, just upload it to another file like below.

$ expand ostechnix.txt>output.txt

We can also convert tabs to spaces, reading from standard input. To do so, just run "expand" command without mentioning the source file name:

$ expand

Just type the text and hit ENTER to convert tabs to spaces. Press CTRL+C to quit.

If you do not want to convert tabs after non blanks, use -i flag like below.

$ expand -i ostechnix.txt

We can also have tabs a certain number of characters apart, not 8 (the default value):

$ expand -t=5 ostechnix.txt

You can even mention multiple tab positions with comma separated like below.

$ expand -t 5,10,15 ostechnix.txt

Or,

$ expand -t "5 10 15" ostechnix.txt

For more details, refer man pages.

$ man expand

Unexpand Command Examples

The Unexpand command will do the opposite of the Expand command. It will convert SPACE characters to TAB characters. Let me show you a few examples to learn how to use Unexpand command.

To convert blanks (spaces, of course) in a file to tabs and write the output to stdout, do:

$ unexpand ostechnix.txt

If you want to write the output in a file instead of just displaying it to stdout, use this command:

$ unexpand ostechnix.txt>output.txt

Convert blanks to tabs, reading from standard output:

$ unexpand

By default, Unexpand command will only convert the initial blanks. If you want to convert all blanks, instead of just initial blanks, use -a flag:

$ unexpand -a ostechnix.txt

To convert only leading sequences of blanks (Please note that it overrides -a):

$ unexpand --first-only ostechnix.txt

Have tabs a certain number of characters apart, not 8 (enables -a):

$ unexpand -t 5 ostechnix.txt

Similarly, we can mention multiple tab positions with comma separated like below.

$ unexpand -t 5,10,15 ostechnix.txt

Or,

$ unexpand -t "5 10 15" ostechnix.txt

For more details, refer man pages.

$ man unexpand

Suggested read:


When you working on large number of files, the Expand and Unexpand commands could be very helpful to replace unwanted TAB characters with SPACE characters and vice versa.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More