Linux expand Command Tutorial For Beginners (with Examples)

While working on the command line in Linux, there may arise a situation where-in the requirement is to replace tabs in a file with spaces. The task isn't difficult if the file is small, but what if the file you're dealing with is huge, or worse, there are hundreds of files in which you have to make this change?

Worry not, there exists a command line tool that's specifically built for this work. The tool in question is expand, and in this tutorial, we will discuss the basics of expand using some easy to understand examples. But before we do that, it's worth sharing that all commands and instructions mentioned here have been tested on Ubuntu 16.04LTS.

Linux expand command

The expand command, as already mentioned, lets you convert tabs to spaces. Following is the syntax of the command:

expand [OPTION]... [FILE]...

Here's how the tool's man page defines it:

Convert tabs in each FILE to spaces, writing to standard output. With no FILE, or when FILE is -, 
read standard input.

The following Q&A-style examples should give you a better idea of how the expand command works.

Q1. How to convert tabs to spaces using expand command?

The basic usage of expand is very simple: pass the name of the file (that cotains tabs) as an argument to the tool.

For example:

expand file1

That's it. The tool will produce the content of the file in output, with the only change being spaces instead of tabs. Note that in case you want to make such a change in multiple files, just pass all the file names in input, and tabs will be converted to spaces in all files in one go.

Of course, you can easily transfer the output to some other file using the redirection operator.

expand file1>file2

Q2. How limit expand's impact to only initial tabs?

Sometimes, you might only want to convert tabs that preceed lines, and leave as it is those that appear after non blanks. The expand command lets you do this as well, just that you'll have to use the -i command line option.

expand -i file1

Q3. How to tweak number of spaces tabs get converted into?

By default, expand converts tabs into the corresponding number of spaces. However, if you want, you can tweak the number of spaces using the -t command line option. This option requires you to enter the new number of spaces.

For example, in our case, tabs get converted into 4 spaces by default. So, in order to reduce these spaces to 1, we used the following command:

expand -t1 file1

Please note that when no file is passed as input to this tool, or you pass a hyphen '-' as file name, the input content is read from standard input (STDIN).

Conclusion

The exapnd command offers a limited set of features, but given that the tool is created for a very specific purpose, the options it provides are more than enough. Just practice whatever you have read, and you should be able to use the tool right away. Those interested, can access the command's man page here.

Share this page:

0 Comment(s)