Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App

fmt command in Linux with examples

Last Updated : 05 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax of fmt command:

syntax of `fmt` command in Linux

fmt [-WIDTH] [OPTION]... [FILE]...

where, the -WIDTH is an abbreviated firm of --width=DIGITS and OPTION refers to the options compatible with the fmt command and FILE refers to the file name. If no FILE is specified, or if FILE is a dash("-"), fmt reads from the standard input.

Using fmt command

fmt by default with no option used format all the words present in the given file in a single line.

cat kt.txt

Used `cat` command to view the content in this file name `kt.txt`

fmt kt.txt

Used `fmt` command to view the content in single line.

fmt kt.txt
fmt kt.txt

To save or write the formatted output you can use fmt as:

fmt kt.txt > dv.txt

Here we have use `>` to save the output in filename `dv.txt`

fmt kt.txt > dv.txt
fmt kt.txt > dv.txt

Options for fmt command

`-w, - -width=WIDTH` option in `fmt` command in Linux

By default, the maximum width is 75 that fmt command produces in output but with the help of -w option it can be changed, it just requires a numerical value for the width you want to specify.

fmt -w 10 dv.txt

Replace your desired filename `dv.txt`

fmt -w 10 dv.txt
fmt -w 10 dv.txt

`-t, - -tagged-paragraph` option in `fmt` command in Linux 

There can be a need for highlighting the very first line in a text file which can be done by making the indentation of first line different from the other lines which can be done with -t command.

$cat kt.txt hello everyone. Have a nice  and prosperous day.  /*-t makes the indentation    of first line different    from others */ $fmt -t kt.txt hello everyone. Have a nice    and prosperous day.

`-s` option in``fmt` command in Linux

This option splits long lines, but doesn't refill them.

$cat kt.txt Love is patient, love is kind. It does not envy,  it does not boast, it is not proud. It is not rude,  it is not self-seeking, it is not easily angered,  it keeps no record of wrongs. Love does not delight  in evil but rejoices with the truth. It always protects,  always trusts, always hopes, always perseveres.  Love never fails.   /* long lines get splitted with -s option */ $fmt -s kt.txt Love is patient, love is kind. It does not envy, it does not boast, it is not proud. It is not rude, it is not self-seeking,  it is not easily angered, it keeps no record of wrongs. Love does not delight in evil but rejoices with the truth. It always protects, always trusts, always hopes, always perseveres.  Love never fails.

`-u, - -uniform-spacing` option in `fmt` command in Linux

This option uses one space between words and two spaces after sentences for formatting.

$cat kt.txt Love   is   patient,   love is   kind.     It does   not envy, it   does not boast,  it is not   proud.   /* Spaces are uniformed with -u option */ $fmt -u kt.txt Love is patient, love is kind.  It does not envy, it does not boast, it is not proud. 

`-c, - -crown-margin` option in `fmt` command in Linux

This option preserves the indentation of the first two lines.

-p, - -prefix=STRING option in `fmt` command in Linux

This option takes STRING as an argument and reformat only lines beginning with STRING, reattaching the prefix to reformatted lines.

-g, - -goal=WIDTH option in `fmt` command in Linux

This option refers to the goal width i.e default of 93% of width.

`- -help` option in `fmt` command in Linux

This displays a help message and exit.

fmt --help
fmt --help
fmt --help

`- -version` option in `fmt` command in Linux

 This displays version information and exit.

fmt --version
fmt --version
fmt --version

Application of fmt command in Linux

  • fmt lets you format the large text files easily with the options like -u which can be a very difficult task if done manually.
  • fmt also lets you change the default width with the help of -w option.
  • It is a good way to save time when it comes to formatting the files.

Conclusion

In this article we have discussed `fmt` command in Linux, which provides a convenient way to format text file effectively. We discussed that it can be used for splitting the long lines, enforce uniform spacing, handle tagged paragraph, or customize the width. Overall, we can say that after understanding this article we can enhance the readability and appearance of our text files, saving time and efforts in the process 


D

Dimpy Varshni
Improve
Article Tags :
  • Misc
  • Linux-Unix
  • linux-command
  • Linux-file-commands
Practice Tags :
  • Misc

Similar Reads

    export command in Linux with Examples
    The 'export' command is one of the essential built-in commands in the Bash shell, allowing users to manage environment variables effectively. It is defined in POSIX standards, which state that the shell will assign the export attribute to specified variables, causing them to be included in the envir
    2 min read
    expr command in Linux with examples
    The expr command in Unix is a versatile tool used for evaluating expressions in the shell environment. It performs a wide range of functions, including arithmetic calculations, string operations, and comparisons based on regular expressions.What is the 'expr' Command?expr stands for "expression" and
    3 min read
    factor command in Linux with examples
    The factor command in Linux is used to print the prime factors of the given numbers, either given from the command line or read from standard input. The numbers given through standard input may be delimited by tabs, spaces or newlines. Syntaxfactor [NUMBER]You can provide one or more numbers as argu
    2 min read
    fc Command in Linux with Examples
    As we all know that LINUX is command friendly and while working on LINUX, you may deal with very long commands that may include long paths or really difficult syntax, and imagine what if working with such commands you do a minor mistake which will require re-writing of the entire command synopsis an
    3 min read
    fc-cache command in Linux with Examples
    The fc-cache command in Linux scans the font directories and build font cache for applications which use fontconfig for their font handling. When no arguments are passed fc-cache scans each directory for font files readable by FreeType. A cache is created which contains properties of each font and t
    2 min read
    fc-list command in Linux with examples
    fc-list command is a part of the fontconfig system. It is used to list the available fonts and font styles. Using the format option, the list of all fonts can be filtered and sorted out. Moreover, multiple parameters can be passed to it after a colon symbol (:) to restrict the information that is be
    3 min read
    fdisk command in Linux with examples
    The 'fdisk' command in Linux, short for "format disk," is a powerful, dialog-driven utility used for creating and manipulating the disk partition table. Essential for system administrators and users looking to manage their hard drive’s structure, 'fdisk' supports a range of operations such as creati
    5 min read
    fg command in Linux with examples
    The fg command in Linux is used to bring a background job into the foreground. It allows you to resume a suspended job or a background process directly in the terminal window, so you can interact with it.Syntaxfg [job_spec]The job_spec is a way to refer to the background jobs that are currently runn
    3 min read
    fgrep command in Linux with examples
    The 'fgrep' filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings that contain lots of regular expression metacharacters, such as "^", "$", etc. This makes 'fgrep' particularly v
    4 min read
    file command in Linux with examples
    The 'file' command in Linux is a vital utility for determining the type of a file. It identifies file types by examining their content rather than their file extensions, making it an indispensable tool for users who work with various file formats. The file type can be displayed in a human-readable f
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences