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