
groff Command in Linux
groff stands for "GNU troff," where "G" stands for GNU, and "troff" is an old typesetting system. It is usually used to generate manual pages and other documentation. But it can do more than just create man pages, as it is a classic document processing system mainly used in Unix-like systems to format text and create documents.
The groff command functions like a compiler for the input you provide and produces a formatted document as output. In this tutorial, we will show you how to use the groff command in Linux along with examples.
Table of Contents
Here is a comprehensive guide to the options available with the groff command −
- What is groff Command?
- Installation of groff Command
- Syntax of groff Command
- Options groff Command
- Manual Page of groff Command
- Examples of groff Command in Linux
What is groff Command?
The groff command in Linux lets us format documents for output. This command takes plain text as input along with formatting macros, processes it, and produces formatted output in various formats. These formats include PostScript, PDF, or HTML.
Installation of Groff Command
The groff command is normally not pre-installed on all Linux distributions by default. However, it can be easily installed using the distribution package manager, such as apt, Pacman, or DNF. For example, on Ubuntu/Debian, it can be installed with the following command −
sudo apt install groff
On Fedora, you can use the following command to install groff −
sudo dnf install groff
Similarly, users can use the Pacman package manager to install groff on Arch Linux −
sudo pacman -S groff
After installing groff on Linux, users can use it to format text and documents.
Syntax of Groff Command
The following syntax is used to run the groff command in Linux −
groff [options] [file ...]
Replace the file with a valid file to format documents for output.
Options Groff Command
The groff command lets us specify various options to customize the formatted output −
Option | Description |
---|---|
[-abcegijklpstzCEGNRSUVXZ] | These flags specify how groff operates. |
[-d cs] | This option sets a predefined string or variable. |
[-D arg] | It specifies the output device. |
[-f fam] | It specifies a font family. |
[-F dir] | It specifies a directory for macro files. |
[-I dir] | It includes files from a specified directory. |
[-K arg] | It specifies the input encoding. |
[-L arg] | It specifies the directory where output will be saved. |
[-m name] | It loads a specific macro package. |
[-M dir] | It specifies the directory for macro files. |
[-n num] | It sets a specific number for the document, useful for defining its version, issue, chapter, etc. |
[-o list] | It defines a list of output files. |
[-P arg] | It passes additional arguments to the output device. |
[-r cn] | It defines a register. |
[-T dev] | It specifies the output device type. |
[-w name] | It sets a warning level. |
[-W name] | It specifies an option for output. |
-h, --help | It returns the help page for the groff command. |
-v | --version | It shows the version number of the groff command. |
Manual Page of Groff Command
You can run the below-given command to access the manual page of groff −
man groff

Examples of groff Command in Linux
In this section, we will discuss several basic to advanced examples of the groff command in Linux −
- Formatting a File with Default Settings
- Formatting an HTML File With groff Command
- Redirecting the Output of the Groff Command to a PostScript File
- Adding Vertical Space in Groff
- Previewing Output as a Plain Text
- Using a Different Macro Package
Formatting a File with Default Settings
Run the groff file without specifying any option with it to format a file with default settings −
groff exampleF.html

Formatting an HTML File With groff Command
Lets run the groff command without any option to format an HTML file −
groff -Thtml exampleF.html
This command processes the file named exampleF.html using groff and outputs it in HTML format −

Redirecting the Output of the Groff Command to a PostScript File
You can run the groff command with the -Tps option to redirect the output to the PostScript file. The following command formats the input file named exampleF.html using Groff and saves the formatted output as a PostScript file named outputFile.ps −
groff -Tps exampleF.html > outputF.ps

Adding Vertical Space in Groff
You can use the groff command with the .sp command to add a specified amount of vertical space before the next line. Heres a simple example −
echo ".sp 5\nWelcome to tutorialspoint.com." | groff -Tpdf -ms > output.pdf

Similarly, you can use groff with .br to add a line break, .ls to adjust line spacing, .bp to break the page, .ce to center your text, .in to set indentation, .ti to break the line and center the next one, .po to set page margins, and .ps to change the font size. These commands together serve as a powerful toolkit for text formatting.
Previewing Output as a Plain Text
You can use the -Tascii option with the groff command to preview the output of a formatted file as plain text −
groff -Tascii exampleF.html

Using a Different Macro Package
We can specify a different macro package to format a file. For instance, in the following command, we use the man package to format a file named exampleF.html −
groff -man exampleF.html

This sums up the working of the groff command in Linux.
Conclusion
In this tutorial, we covered the groff command, which is a useful tool in Linux for formatting documents like manual pages. It works like a compiler, taking plain text and turning it into formatted output, such as PostScript, PDF, or HTML.
We explained its basic syntax, valid options, and demonstrated several practical examples. These examples showed how to format files, redirect the output to a new file, add vertical space, and preview the result as plain text. Moreover, we discussed how Groffs commands like .sp, .br, and .ps make it easy to format text and documents in Unix-like systems.