unexpand

text processinglinux
The unexpand command is one of the most frequently used commands in Linux/Unix-like operating systems. unexpand The unexpand command converts spaces in files to tabs. It's the counterpart to the expand command and is useful for reducing file size or formatting files according to specific requirements that prefer tabs over spaces.

Quick Reference

Command Name:

unexpand

Category:

text processing

Platform:

linux

Basic Usage:

unexpand [options] [arguments]

Common Use Cases

  • 1

    Space compression

    Convert spaces to tabs in text data

  • 2

    Text processing

    Manipulate text data in pipelines and scripts

  • 3

    Data cleaning

    Clean and sanitize text data

  • 4

    Scripting

    Use in shell scripts to process text data programmatically

Syntax

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

Options

Option Description
-a, --all Convert all sequences of spaces (not just leading spaces) to tabs where possible.
--first-only Convert only leading sequences of spaces on each line (opposite of -a).
-t N, --tabs=N Set tab stops to every N columns instead of the default 8. This determines how many spaces are replaced by each tab.
-t LIST, --tabs=LIST Use a comma-separated list of tab positions (e.g., 2,4,8). Each number specifies a column position for a tab stop.
--help Display help information and exit.
--version Output version information and exit.

Examples

How to Use These Examples

The examples below show common ways to use the unexpand command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

unexpand file_with_spaces.txt

Converts leading spaces in each line to tabs (using the default 8-space tab stops) and outputs the result to standard output.

unexpand -t 4 file_with_spaces.txt

Converts each group of 4 spaces at the beginning of lines to a tab. This is useful when working with code that uses 4-space indentation but you want to store it with tabs.

unexpand -t 4 file_with_spaces.txt > file_with_tabs.txt

Converts spaces to tabs (with 4-space tab stops) and saves the result to a new file instead of displaying it on screen.

cat file_with_spaces.txt | unexpand -t 2

Reads input from a pipe instead of a file, converting groups of 2 spaces to tabs. This demonstrates how unexpand can work within command pipelines.

Advanced Examples:

unexpand -a file_with_spaces.txt

Converts all spaces, not just leading ones, to tabs when possible. This maximizes tab usage throughout the file for potentially greater space savings.

unexpand -t 4 --first-only file_with_spaces.txt

Converts only the spaces at the beginning of each line to tabs, using 4 spaces per tab. This is useful for handling indentation while leaving inline spacing intact.

unexpand -a -t 4,8,12,16 file_with_spaces.txt

Sets custom tab stops at columns 4, 8, 12, and 16, and converts all spaces (not just leading ones) to tabs when possible. This gives precise control over how spaces are converted.

find . -name "*.txt" -exec cat {} \; | unexpand -t 4 > all_unexpanded.txt

Finds all .txt files in the current directory and its subdirectories, concatenates their content, converts spaces to tabs (using 4-space tab stops), and combines the output into a single file.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

What Unexpand Does:

  • Converts space characters to tabs based on the specified tab stops
  • Reduces file size by replacing multiple spaces with single tab characters
  • Preserves text alignment while making the file more compact
  • Can process multiple files in a single operation
  • Works both on files and standard input (via pipes)

Common Use Cases:

  • Optimizing file size by replacing multiple spaces with single tabs
  • Converting indentation from spaces to tabs according to project style guides
  • Preparing files for environments that work better with tabs than spaces
  • Standardizing file formats in codebases with specific tab requirements
  • Converting between different text format conventions

How Space to Tab Conversion Works:

  • By default, unexpand only converts leading spaces at the beginning of each line
  • With the -a option, it converts all appropriate sequences of spaces throughout the file
  • Each sequence of spaces that reaches a tab stop gets converted to a tab
  • Spaces that don't reach the next tab stop remain as spaces
  • Tab stops by default are every 8 columns (1, 9, 17, 25, etc.)
  • Custom tab stops can be specified for specialized formatting needs

File Handling:

  • When no file is specified, unexpand reads from standard input
  • With multiple files, each file is processed in sequence
  • Output is written to standard output by default, so redirection (>) is needed to save to a file
  • Unexpand does not modify the original files unless output is explicitly redirected back to the input file

Related Commands:

  • expand - Convert tabs to spaces (the opposite of unexpand)
  • col - Filter reverse line feeds and handle whitespace conversion
  • tr - Translate characters, including replacement of spaces with tabs
  • sed - Stream editor that can perform complex text transformations
  • pr - Format text files for printing, handling tab stops
  • fmt - Simple text formatter that can work with tabs

Tips & Tricks

1

Use the -a option to convert all blanks to tabs

2

Use the -t number option to specify the number of spaces per tab

3

Use the -t option without a number to convert initial spaces to tabs

4

Use the -h option to display help

5

Use the -v option to display version information

Common Use Cases

Space compression

Convert spaces to tabs in text data

Text processing

Manipulate text data in pipelines and scripts

Data cleaning

Clean and sanitize text data

Scripting

Use in shell scripts to process text data programmatically

File format conversion

Convert between different file formats

Related Commands

These commands are frequently used alongside unexpand or serve similar purposes:

Use Cases

1

Space compression

Convert spaces to tabs in text data

2

Text processing

Manipulate text data in pipelines and scripts

3

Data cleaning

Clean and sanitize text data

4

Scripting

Use in shell scripts to process text data programmatically

5

File format conversion

Convert between different file formats

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the unexpand command works in different scenarios.

$ unexpand
View All Commands