Skip to content

fmt Command — Formatting Text in Linux

The fmt command is used to reformat and wrap text into neatly formatted paragraphs. It is especially useful for preparing documentation, README files, reports, emails, configuration notes, and other text files where readability matters.


Learning Path

Linux Mastery → Module 3: Text Processing → Lesson 11

Difficulty: Beginner → Intermediate

Reading Time: 20 Minutes

Course Progress

Course: Linux Mastery

Module: Text Processing

Lesson: 11 of 18


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand the fmt command
  • Format long paragraphs
  • Set custom line widths
  • Preserve indentation
  • Format multiple files
  • Use fmt in command pipelines
  • Prepare professional text documents

Prerequisites

Before starting this lesson, complete:

  • Module 1 – Linux Fundamentals
  • Module 2 – Linux Command Line Essentials
  • Module 3 Lessons 1–10

Why Learn fmt?

Imagine someone sends you a text file like this:

Linux is an open-source operating system that powers servers, cloud infrastructure, supercomputers, embedded systems, and millions of devices around the world. Reading long lines without proper formatting is difficult.

It's hard to read.

Use:

fmt document.txt

Output:

Linux is an open-source operating system that powers
servers, cloud infrastructure, supercomputers,
embedded systems, and millions of devices around the
world. Reading long lines without proper formatting is
difficult.

What is fmt?

The fmt command reformats text by wrapping long lines into paragraphs of a specified width.

Syntax:

fmt [OPTIONS] FILE

Sample File

Create:

cat > article.txt

Paste:

Linux is one of the most powerful operating systems available today. It is widely used in cloud computing, DevOps, cybersecurity, networking, embedded systems, artificial intelligence, and enterprise infrastructure.

Press:

Ctrl + D

Basic Formatting

fmt article.txt

Output:

Linux is one of the most powerful operating systems
available today. It is widely used in cloud computing,
DevOps, cybersecurity, networking, embedded systems,
artificial intelligence, and enterprise infrastructure.

Specify Line Width

Wrap text at 40 characters.

fmt -w 40 article.txt

Output:

Linux is one of the most powerful
operating systems available today.
It is widely used in cloud
computing, DevOps,
cybersecurity, networking,
embedded systems, artificial
intelligence, and enterprise
infrastructure.

Wrap text at 80 characters.

fmt -w 80 article.txt

80 characters is a common width for documentation and terminal output.


Format Standard Input

echo "Linux is amazing for cloud computing and automation." | fmt

Output:

Linux is amazing for cloud computing and automation.

Save Formatted Output

fmt article.txt > formatted.txt

Display:

cat formatted.txt

Format Multiple Files

fmt file1.txt file2.txt

Preserve Prefixes

Suppose a file contains comments.

# Linux is a powerful operating system used in many environments.
# It supports servers, desktops, and cloud platforms.

Format while preserving the comment prefix.

fmt -p "#" comments.txt

Output:

# Linux is a powerful operating system used in many
# environments. It supports servers, desktops, and
# cloud platforms.

The -p option is useful when formatting source code comments or documentation.


Combine with Other Commands

Format command output.

cat article.txt | fmt

Format log summaries.

grep ERROR application.log | fmt

Wrap text before saving.

cat notes.txt | fmt > report.txt

Common fmt Options

Option Description
-w Set line width
-p Preserve line prefix
-u Uniform spacing

Uniform Spacing

Remove extra spaces between words.

Example:

Linux    is     powerful.

Command:

fmt -u file.txt

Output:

Linux is powerful.

Real Production Examples

Format release notes.

fmt release-notes.txt

Prepare documentation.

fmt README.md

Format email templates.

fmt email.txt

Wrap generated reports.

cat report.txt | fmt -w 80

Format code comments.

fmt -p "#" script-comments.txt

Production Perspective

Although fmt is not used as frequently as grep or awk, it is valuable for:

  • Technical documentation
  • README files
  • Release notes
  • Email templates
  • Script comments
  • Text report formatting

It improves readability and ensures consistent formatting.


Hands-on Lab

Task 1

Create:

cat > notes.txt

Paste a long paragraph.


Task 2

Format it.

fmt notes.txt

Task 3

Wrap at 50 characters.

fmt -w 50 notes.txt

Task 4

Save formatted output.

fmt notes.txt > output.txt

Task 5

Display output.

cat output.txt

Task 6

Format using standard input.

echo "Linux powers the cloud." | fmt

Task 7

Create a comment file.

# Linux is secure and scalable.
# It powers modern cloud platforms.

Format:

fmt -p "#" comments.txt

Task 8

Remove extra spaces.

fmt -u notes.txt

Command Deep Dive

Command Purpose Production Example
fmt file.txt Format text Documentation
fmt -w 60 Wrap at 60 columns Reports
fmt -p "#" Preserve comments Source code
fmt -u Normalize spacing Email templates

Production Troubleshooting Scenario

Scenario

A DevOps engineer generates a deployment report from a script.

The report contains long, unreadable lines.

Tasks:

  1. Wrap the report at 80 columns.
  2. Preserve comment lines.
  3. Save the formatted report.
  4. Verify the output.

Solutions:

fmt -w 80 deployment-report.txt

fmt -p "#" deployment-report.txt

fmt deployment-report.txt > deployment-report-formatted.txt

cat deployment-report-formatted.txt

Mini Challenge

Create:

guide.txt

Paste a long paragraph describing Linux.

Perform the following:

  • Format the text.
  • Wrap at 60 columns.
  • Wrap at 100 columns.
  • Save the formatted version.
  • Create a comment file and preserve comment prefixes.
  • Normalize multiple spaces.

Best Practices

  • Use 80-character line widths for terminal-friendly documentation.
  • Preserve prefixes when formatting comments.
  • Keep documentation consistently formatted.
  • Review formatted output before publishing.
  • Use fmt for plain text, not structured formats such as JSON or YAML.

Common Mistakes

❌ Using fmt on structured data.

✅ Avoid formatting:

JSON

YAML

XML

It may break the structure.


❌ Assuming fmt preserves every line exactly.

✅ It is designed to reflow paragraphs, not preserve manual line breaks.


❌ Using fmt on Markdown tables.

✅ Tables may become misaligned.


Interview Questions

Beginner

  1. What is the purpose of the fmt command?
  2. What does the -w option do?
  3. How do you save formatted output to another file?
  4. What is the default behavior of fmt?

Intermediate

  1. Explain the -p option.
  2. What does fmt -u do?
  3. Why shouldn't fmt be used on YAML files?
  4. When is fmt useful in shell scripting?

Architect Level

  1. How would you automatically format generated documentation in a CI/CD pipeline?
  2. Why is consistent text formatting important in technical documentation?
  3. How can fmt improve the readability of generated reports?

Summary

In this lesson, you learned:

  • Formatting paragraphs
  • Wrapping long lines
  • Setting custom line widths
  • Preserving prefixes
  • Formatting standard input
  • Preparing documentation

The fmt command is a lightweight utility that makes plain-text documents more readable. While it isn't used as frequently as commands like grep or awk, it is valuable for documentation, reports, and automation workflows that generate human-readable text.


Key Takeaways

  • fmt wraps long lines into readable paragraphs.
  • Use -w to specify line width.
  • Use -p to preserve prefixes such as comment markers.
  • Use -u to normalize spacing.
  • Avoid using fmt on structured data formats like JSON, YAML, or Markdown tables.

What's Next?

column Command — Displaying Text in Tabular Format

In the next lesson, you'll learn:

  • Aligning text into columns
  • Formatting CSV files
  • Creating readable tables
  • Working with command output
  • Generating professional terminal reports