Linux expand and unexpand commands

Updated: 06/16/2017 by Computer Hope

About expand and unexpand

expand copies files (or the standard input) to the standard output, with tab characters expanded to space characters. Backspace characters are preserved into the output and decrement the column count for tab calculations.

expand is useful for pre-processing character files (before sorting, looking at specific columns, and so forth) that contain tab characters.

expand and unexpand syntax

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

Expand Options

-i, --initial do not convert tabs after non-blanks.
-t, --tabs=NUMBER set tabs NUMBER characters apart, instead of the default of 8.
-t, --tabs=LIST use comma-separated list of explicit tab positions.
--help Display a help message and exit.
--version Display version information and exit.

Unexpand Options

-a, --all convert all blanks, instead of just initial blanks.
--first-only convert only leading sequences of blanks (overrides -a).
-t, --tabs=N set tabs N characters apart instead of the default of 8 (enables -a).
-t, --tabs=LIST use comma separated LIST of tab positions (enables -a).
--help display a help message and exit.
--version Display version information and exit.

expand and unexpand examples

expand myfile.txt

Expand the file myfile.txt, changing tabs to spaces, and display on standard output.

expand --tabs=10 myfile.txt > myfile2.txt

Converts the tabs in the file myfile.txt to 10 spaces each, and write the output to myfile2.txt.

tabs — Set tab stops on a terminal.