Skip to content

fmt Linux Command Guide

The Linux fmt command is a powerful tool for formatting text files. It can adjust line lengths, wrap text, and manage spacing to improve readability. By specifying options such as the maximum line width and indentation levels, users can customize the formatting to meet their specific needs. Whether working with long paragraphs or narrow columnar data, the fmt command can help ensure that text is displayed in a clear and organized manner.

fmt Syntax:

Terminal window
fmt [option] [file]

Options:

OptionDescription
-w WIDTHSpecify the maximum line width
-sSqueeze multiple blank lines into one
-uReindent wrapped lines
-aDo not split lines shorter than width

Parameters:

ParameterDescription
fileSpecifies the file to format

fmt Command Samples:

Format a File with Default Line Width

Terminal window
fmt file.txt

Formats the text in “file.txt” with a default line width of 75 characters.

Format a File with a Custom Line Width

Terminal window
fmt -w 60 file.txt

Formats the text in “file.txt” with a custom line width of 60 characters.

Format Multiple Files

Terminal window
fmt file1.txt file2.txt

Formats the text in “file1.txt” and “file2.txt” with default line width.

Read Input from Standard Input

Terminal window
echo "This is a sample sentence that needs formatting" | fmt

Reads the input sentence and formats it using the fmt command.

Format Files Recursively in a Directory

Terminal window
find /path/to/directory -type f -exec fmt {} \;

Recursively formats all text files in a directory and its subdirectories.

Justify Text Alignment

Terminal window
echo "This is a test to justify text alignment using fmt command" | fmt -w 80 -j

Justifies the text alignment of the input sentence with a line width of 80 characters.

Combine Multiple Options

Terminal window
fmt -s -w 60 file.txt

Formats the text in “file.txt” with single spacing and a line width of 60 characters.

fmt FAQ:

How do I use fmt in Linux?

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

Terminal window
fmt file.txt

What is the purpose of the fmt command in Linux?

The fmt command in Linux is used to format text files to a specific width by breaking lines that exceed the specified width.

Terminal window
fmt -w 80 file.txt

How can I adjust the width for text formatting with fmt in Linux?

You can adjust the width for text formatting in fmt using the -w option followed by the desired width value.

Terminal window
fmt -w 60 file.txt

How do I handle hyphenated words when using fmt in Linux?

To preserve hyphenated words and prevent line breaks within them, you can use the -c option with fmt.

Terminal window
fmt -c file.txt

Can I number non-blank output lines with fmt in Linux?

Yes, you can number non-blank output lines by using the -n option with fmt.

Terminal window
fmt -n file.txt

How to remove extra spaces and concatenate lines with fmt in Linux?

To remove extra spaces and concatenate lines when formatting text with fmt, use the -s option.

Terminal window
fmt -s file.txt

Applications of the fmt command

  • Reformat text files to a specified line length.
  • Wrap long lines of text to a specific width.
  • Improve the readability of text by adjusting line lengths.
  • Prepare text for printing with a desired width.
  • Standardize the formatting of text files.