unexpand - Linux


The unexpand command in Linux is used to convert spaces in a file to tabs, which can help in reducing file size and standardizing whitespace characters, especially in code files where indentation is space-based but could be efficiently represented with tabs. This is particularly useful in environments where space conservation is critical, or a uniform tab representation is required for code styles and standards.


The basic syntax of the unexpand command is as follows:

unexpand [OPTIONS]... [FILE]...

unexpand reads from standard input if no file is specified or if the file argument is -.


Here are the most commonly used options in unexpand:

  • --first-only, -a: Convert only leading blanks (spaces and tabs at the beginning of the line) into tabs, leaving other blanks unchanged.
  • --tabs=NUM: Set the number of spaces corresponding to a tab to NUM defaults. The default is 8. Tabs can be set as a list.
  • --tabs=LIST: Specify tab stops. For example, --tabs=4,20,35 sets tabs at these specific column points.
  • --help: Display help information and exit.
  • --version: Output version information and exit.


  1. Convert spaces to tabs:

    unexpand file.txt > output.txt

    This command converts spaces to tabs in file.txt and writes the result to output.txt.

  2. Convert only leading spaces to tabs on each line:

    unexpand -a file.txt > output.txt

    Converts only the leading spaces of each line in file.txt to tabs.

  3. Set custom tab spaces:

    unexpand --tabs=4 input.txt > output.txt

    Replace spaces with tabs assuming every 4 spaces equal one tab.

Common Issues

  • Incorrect formatting: Improper tab spacing can disrupt the alignment in the output file, especially in structured documents like tables or code with aligned comments.
  • Data interpretation: Some applications may not interpret tabs uniformly, which can lead to misalignment. Testing output on different systems or applications is advisable.


unexpand is often used in scripting to preprocess files before further processing with tools like awk, sed, or compilers.

Example usage in a script:

for file in *.py; do
    unexpand --tabs=4 "$file" > "converted_$file"

This script converts the leading spaces to tabs in all Python .py files in the current directory, each saved with a converted_ prefix.

  • expand: Converts tabs in files to spaces. It is the direct opposite of unexpand.
  • sed: Stream editor for filtering and transforming text, often used with unexpand for complex transformations.

Further reading and more detailed information about unexpand can be accessed via the Linux man pages (try man unexpand in your terminal), or the GNU coreutils page: GNU Coreutils.