Open In App

unexpand command in Linux with Examples

To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. 

The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here’s the syntax of unexpand command : 
Syntax : 



$unexpand [OPTION]... [FILE]...

where, OPTION refers to the options compatible with unexpand and FILE refers to the file name. 

 



Using unexpand command

To convert all the space characters into tab characters in the file kt.txt , use unexpand as : 

$cat -vet kt.txt
have    a    nice    day$
always    try    harder$
to    achieve    better$


/* In the below output $ refers to 
   the new line feed and ^I refers 
   to the tab */
$unexpand kt.txt
have^Ia^Inice^Iday$
always^Itry^Iharder$
to^Iachieve^Ibetter$

In order to save the produced output by unexpand command in another file let’s say dv.txt use the below syntax : 

/* Saving output in file, dv.txt */
$unexpand kt.txt>dv.txt

$cat -vet dv.txt
have^Ia^Inice^Iday$
always^Itry^Iharder$
to^Iachieve^Ibetter$

Options for unexpand command

/* This converts all the blanks 
   also into tabs */
$unexpand -a kt.txt>dv.txt
/* This converts only the leading
   sequences of blanks */
$unexpand --first-only kt.txt>dv.txt
/* the -t option with numerical value 
   2 forces to change the spaces
   into tabs of only 2 characters */
$unexpand -t2 kt.txt>dv.txt

Also see : expand command  

Article Tags :