fmt Command — Formatting Text in Linux¶
The
fmtcommand 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¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
fmtcommand - Format long paragraphs
- Set custom line widths
- Preserve indentation
- Format multiple files
- Use
fmtin 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:
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:
Sample File¶
Create:
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:
Basic Formatting¶
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.
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.
80 characters is a common width for documentation and terminal output.
Format Standard Input¶
Output:
Save Formatted Output¶
Display:
Format Multiple Files¶
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.
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.
Format log summaries.
Wrap text before saving.
Common fmt Options¶
| Option | Description |
|---|---|
-w | Set line width |
-p | Preserve line prefix |
-u | Uniform spacing |
Uniform Spacing¶
Remove extra spaces between words.
Example:
Command:
Output:
Real Production Examples¶
Format release notes.
Prepare documentation.
Format email templates.
Wrap generated reports.
Format code comments.
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:
Paste a long paragraph.
Task 2¶
Format it.
Task 3¶
Wrap at 50 characters.
Task 4¶
Save formatted output.
Task 5¶
Display output.
Task 6¶
Format using standard input.
Task 7¶
Create a comment file.
Format:
Task 8¶
Remove extra spaces.
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:
- Wrap the report at 80 columns.
- Preserve comment lines.
- Save the formatted report.
- 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:
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
fmtfor plain text, not structured formats such as JSON or YAML.
Common Mistakes¶
❌ Using fmt on structured data.
✅ Avoid formatting:
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¶
- What is the purpose of the
fmtcommand? - What does the
-woption do? - How do you save formatted output to another file?
- What is the default behavior of
fmt?
Intermediate¶
- Explain the
-poption. - What does
fmt -udo? - Why shouldn't
fmtbe used on YAML files? - When is
fmtuseful in shell scripting?
Architect Level¶
- How would you automatically format generated documentation in a CI/CD pipeline?
- Why is consistent text formatting important in technical documentation?
- How can
fmtimprove 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¶
fmtwraps long lines into readable paragraphs.- Use
-wto specify line width. - Use
-pto preserve prefixes such as comment markers. - Use
-uto normalize spacing. - Avoid using
fmton 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