expand

text processinglinux
The expand command is one of the most frequently used commands in Linux/Unix-like operating systems. expand The expand command converts tabs in files to spaces. It's useful for ensuring consistent text formatting and making files more compatible with various text editors or display environments that might handle tabs differently.

Quick Reference

Command Name:

expand

Category:

text processing

Platform:

linux

Basic Usage:

expand [options] [arguments]

Common Use Cases

  • 1

    Tab expansion

    Convert tabs to spaces 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

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

Options

Option Description
-i, --initial Only convert tabs at the beginning of lines. Tabs elsewhere in the line remain unchanged.
-t N, --tabs=N Set tab stops to every N columns instead of the default 8. This determines how many spaces replace 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 expand command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

expand file_with_tabs.txt

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

expand -t 4 file_with_tabs.txt

Converts tabs to 4 spaces instead of the default 8. This is useful when working with code that uses 4-space indentation.

expand -t 4 file_with_tabs.txt > file_with_spaces.txt

Converts tabs to 4 spaces and saves the result to a new file instead of displaying it on screen.

cat file_with_tabs.txt | expand -t 2

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

Advanced Examples:

expand -i file_with_tabs.txt

Only convert initial tabs on each line to spaces, preserving any tabs that occur after non-whitespace characters. This is useful for files where tabs are used for both indentation and data alignment.

expand -t 4,8,12,16 file_with_tabs.txt

Sets custom tab stops at columns 4, 8, 12, and 16. This gives precise control over how tabs are converted, which can be useful for preserving alignment in structured text files.

find . -name "*.txt" -exec expand -t 4 {} \; > expanded_files.txt

Finds all .txt files in the current directory and its subdirectories, converts their tabs to 4 spaces, and combines the output into a single file. This shows how expand can be used in batch processing scenarios.

expand -t 4 file1.txt file2.txt file3.txt > all_files_expanded.txt

Processes multiple files in a single command and combines them into one output file with all tabs converted to 4 spaces.

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 Expand Does:

  • Converts tab characters to the appropriate number of spaces based on tab stops
  • Helps standardize text formatting across different systems and editors
  • Preserves text alignment while making it more consistent
  • Can process multiple files in a single operation
  • Works both on files and standard input (via pipes)

Common Use Cases:

  • Preparing text files for environments that handle tabs poorly
  • Standardizing indentation in source code files
  • Ensuring consistent formatting in documentation
  • Converting tab-delimited data to fixed-width format
  • Making text files more readable in environments where tab width is inconsistent

How Tab Expansion Works:

  • By default, tab stops are set at every 8th column (1, 9, 17, 25, etc.)
  • Each tab character moves the cursor to the next tab stop
  • The command replaces each tab with enough spaces to reach the next tab stop
  • Custom tab stops can be specified for special formatting needs
  • The -i option distinguishes between tabs used for indentation and those used for alignment

File Handling:

  • When no file is specified, expand 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
  • Expand does not modify the original files unless output is explicitly redirected back to the input file

Related Commands:

  • unexpand - Convert spaces to tabs (the opposite of expand)
  • col - Filter reverse line feeds and handle whitespace conversion
  • tr - Translate characters, including replacement of tabs with spaces
  • sed - Stream editor that can replace tabs with spaces using 's/\t/ /g'
  • pr - Format text files for printing, can handle tab expansion
  • fmt - Simple text formatter that handles tabs

Tips & Tricks

1

Use the -i option to ignore non-printable characters

2

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

3

Use the -t option without a number to expand all tabs to spaces

4

Use the -h option to display help

5

Use the -v option to display version information

Common Use Cases

Tab expansion

Convert tabs to spaces 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 expand or serve similar purposes:

Use Cases

1

Tab expansion

Convert tabs to spaces 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 expand command works in different scenarios.

$ expand
View All Commands