fmt command in Linux: A Practical Guide

Introduction

In this tutorial, we’ll explore how to use the fmt command in Linux to neatly format text in files, set optimal column widths, and standardize spacing. This guide aims to enhance your text processing skills on Linux using practical examples of the fmt command. Let’s dive into the capabilities of the fmt command in Linux.

What does the fmt command mean?

fmt – a straightforward command used as an optimal text formatter.

Syntax

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


In the manual page, the fmt command is described as a simple and optimal text formatting Linux. For more detailed information about the fmt command, you can refer to the man fmt section in the manual.

fmt command in linux with example

fmt command in Linux with an example

By default, fmt sets the column width at 75.

I have created a file fmt_command.txt

[vagrant@DevopsRoles ~]$ cat fmt_command.txt 
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 Devops Roles.
 Hello world. xxx.

fmt with no options

[vagrant@DevopsRoles ~]$ fmt fmt_command.txt 
HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.  Devops Roles.

Hello world. xxx.

How to change the width of formatting.

[vagrant@DevopsRoles ~]$ fmt --width 20 fmt_command.txt 
 HuuPV, My website
 DevopsRoles.com and
 HuuPhan.com.SN:199x.
 Devops Roles.
 Hello world. xxx.

with -u option uses one space between words and two spaces after sentences for formatting.

[vagrant@DevopsRoles ~]$ fmt -u fmt_command.txt 
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.  Devops Roles.
 Hello world. xxx.

Formatting Multiple Files

You can also format multiple files simultaneously. Simply list the files as arguments:

fmt file1.txt file2.txt file3.txt

fmt will process each file and output the formatted text for all of them.

Splitting Long Lines

If you need to split long lines without breaking words, use the -s or --split-only option:

fmt -s myfile.txt

This option splits lines at spaces, ensuring words are not cut off.

Advanced Formatting Options

The fmt command offers several advanced options for more precise text formatting:

  • -c or --crown-margin: Preserve the indentation of the first two lines.
  • -t or --tagged-paragraph: Format the text as tagged paragraphs, maintaining a hanging indentation.
  • -g or --goal: Set the goal width for formatting. This is the preferred width, while the -w option sets the maximum width.

Frequently Asked Questions

What is the default width for the fmt command?

The default width is 75 characters. You can change it using the -w or --width option.

Can fmt handle multiple files at once?

Yes, you can list multiple files as arguments and fmt will format each of them.

How can I preserve indentation with fmt?

Use the -u or --uniform-spacing option to keep the original indentation intact.

What is the difference between the -w and -g options?

The -w option sets the maximum line width, while the -g option sets the goal width, which is the preferred width for formatting.

Can fmt split long lines without breaking words?

Yes, use the -s or --split-only option to split lines at spaces without cutting off words.

Conclusion

The fmt command is a straightforward Linux utility that helps format text files, set column width commands, and ensure uniform spacing. It simplifies text editing tasks, making it a valuable tool for developers and system administrators alike. Thank you for reading this guide on the DevopsRoles page!

By understanding and utilizing the various options and features of fmt, you can efficiently format text files to meet your specific requirements. Experiment with the examples provided in this guide and explore the full potential of the fmt command in your Linux environment.

,

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.