Using fmt command in Linux

The fmt command in Linux is used to format each paragraph of a text file. You can manually format the text of a small file but what if the file is larger then formatting manually will be a very hectic and boring task. So in Linux, you can use this tool to easily format the text of a file.

In this article, we will discuss the usage of fmt command along with some examples.

How to use fmt command in Linux

The syntax of using fmt command on a Linux system is given below.

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


WIDTH – is a numeric value that defines the width of lines in a file

OPTION – you can find the list of options that can be used with fmt command on the man page of fmt command

FILE – file is the input file whose text is to be formatted

How to format the content of a file

By default fmt command formats all the content in a single line. The following is the most basic form of fmt command to format a file –

fmt filename

For example –

Suppose we have a file called file1.txt you can see the content of this file in the given image –

Now use the following command to format this file-

fmt file1.txt

You can see the output of this command –

How to change the default line width of a file

You can use the option -w or --width to change the default width of a line which is 75. You need to pass a numeric value to set the new width.

For example –

fmt -w 40 file1.txt

This command will reduce the line width to 40 now you can see the output in the given image –

How to make fmt to split long lines

By using the option -s will split long lines that are present in the content of your file. An example of this is given below –

fmt -s file1.txt

This command will split all the long lines present in file1.txt.

There are several other options are available that you can use with this command. For more details of this command, you can see its manual page by using –

man fmt


Ok, that’s all for now. You can try this command on your own if you have any query then write us in the comments below.

Leave a Comment