Skip to content
Related Articles
Open in App
Not now

Related Articles

expand Command in LINUX with examples

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 18 Oct, 2021
Improve Article
Save Article

Whenever you work with files in LINUX there can be a situation when you are stuck with a file that contains a lot of tabs in it and whatever you need to do with 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 a large number of 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 into the output and also decrement the column count for tab calculations. 

Syntax of expand : 
 

//...syntax of expand...//
$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. 

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

 

//using expand//

$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 get 
transfer to dv.txt as 
redirection operator >
is used*/

 

Options for expand command:

1. -i, – – initial option : 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 : 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 : This uses comma separated LIST of tab positions. 

4. – -help : This will display a help message and exit. 

5. –version : 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. 

Also see : unexpand command 

 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!