Linux expand and unexpand commands

Updated: 05/04/2019 by Computer Hope
expand command

On Unix-like operating systems, the expand command 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.

The unexpand command reverses this process.

This page covers the GNU/Linux versions of expand and unexpand.

Description

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

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 only 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.

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.