Skip to content

expand command in Linux

The expand command in Linux is a useful tool for converting tabs in files to spaces. By default, it replaces tabs with spaces in the input files and writes the result to standard output. This command is handy for improving file readability and alignment, especially when sharing files with others or formatting code. It allows users to specify the tab stops, control the number of spaces each tab is replaced with, and choose the number of spaces between tab stops. With the expand command, users can easily customize the transformation of tabs to spaces based on their specific formatting requirements.

expand Syntax:

Terminal window
expand [OPTION] [FILE]

Linux expand Options:

OptionDescription
-t, —tabs=Nset tab stops at N columns
-i, —initialdo not convert tabs after non blanks

expand Parameters:

ParameterDescription
FILEthe input file to be expanded

How to use expand command:

Expand Tab Characters

Terminal window
expand -t 4 file.txt

Replaces tab characters in “file.txt” with 4 spaces.

Expand Multiple Files

Terminal window
expand file1.txt file2.txt

Expands tab characters in both “file1.txt” and “file2.txt”.

Expand Tab Characters Recursively

Terminal window
expand -t 2 -i *.txt

Recursively expands tab characters in all “.txt” files with 2 spaces.

Expand Tab Characters and Save to Output File

Terminal window
expand -t 8 file.txt > output.txt

Replaces tab characters in “file.txt” with 8 spaces and saves the output to “output.txt”.

Expand Multiple Files with Backup

Terminal window
expand -t 4 --backup file1.txt file2.txt

Expands tab characters in “file1.txt” and “file2.txt” with 4 spaces, creating backups.

Expand Tab Characters Verbose Output

Terminal window
expand -t 3 -v file.txt

Displays verbose output while replacing tab characters in “file.txt” with 3 spaces.

Expand Tab Characters in a Specific Column Range

Terminal window
expand -t 4,8 file.txt

Replaces tab characters in columns 4 to 8 in “file.txt” with 4 spaces.

Expand Tab Characters without Replacing Single Spaces

Terminal window
expand -t 4 --tabs=1 file.txt

Expands only tab characters, ignoring single spaces, in “file.txt” with 4 spaces.

How do I use expand in Linux?

To use the expand command in Linux, execute the following command:

Terminal window
expand file.txt

How can I replace tabs with spaces using expand?

To replace tabs with spaces in a file using expand, use the following command:

Terminal window
expand -t 4 file.txt

How do I specify the number of spaces for tab expansion in expand?

To specify the number of spaces for tab expansion in the expand command, use the following syntax:

Terminal window
expand -t 6 file.txt

Can I use expand to process multiple files at once?

Yes, you can use expand to process multiple files simultaneously. Simply provide the list of files as arguments to the command like this:

Terminal window
expand file1.txt file2.txt

How do I display help information for the expand command?

To display help information for the expand command, use the following command:

Terminal window
expand --help

How can I compress multiple spaces into a single space with expand?

To compress multiple spaces into a single space in a file using expand, you can use the following command:

Terminal window
expand -t 1 file.txt

How do I preserve the backspace characters in a file with expand?

To preserve backspace characters while using the expand command in Linux, you can run the command with the -i option like this:

Terminal window
expand -i file.txt

Can I specify a specific output file when using expand?

Yes, you can specify a specific output file when using expand. Use the following command to redirect the output to a new file:

Terminal window
expand file.txt > newfile.txt

Applications of the expand command

  • Expanding tabs in a file
  • Converting tabs to spaces
  • Standardizing tab size in text files