Master the Linux fmt Command: Simple Text Formatting Techniques
This tutorial explains how to use the Linux fmt utility to reformat text, covering basic syntax, line‑width adjustments, first‑line indentation, line splitting, and spacing options with clear examples and command‑line snippets tested on Ubuntu 24.04.
Introduction
The fmt command is a lightweight Linux utility that automatically formats plain‑text files or standard input, making the output more readable by adjusting line width and paragraph layout. By default it wraps lines at 75 characters, but this can be customized.
Basic Syntax
fmt [-WIDTH] [OPTION]... [FILE]...The command reads each paragraph from the specified file(s) (or stdin) and outputs the reformatted text to stdout.
Common Options and Examples
Q1: Format a file into a single line
Running fmt without options concatenates the entire file into one continuous line.
fmt filenameQ2: Change the maximum line width
Use -w followed by a number to set a custom line width.
fmt -w 20 filenameQ3: Highlight the first line
The -t option adds a different indentation to the first line, making it stand out.
fmt -t filenameQ4: Split overly long lines
Apply the -s option to break long lines into shorter, more readable segments.
fmt -s filenameQ5: Set word and sentence spacing
With -u, fmt ensures a single space between words and two spaces between sentences, which is enabled by default.
fmt -u filenameConclusion
Although fmt offers a modest feature set, it remains a powerful tool for quick text reformatting, line‑width control, and consistent paragraph layout. For more advanced usage, consult its manual page.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
