thelinuxvault blog

Mastering the `pr` Command in Linux

In the Linux operating system, there are numerous commands that help users manipulate and format text files efficiently. One such command is pr, which stands for "print". The pr command is used to format text files for printing, dividing the output into pages, adding headers, footers, and columns. It is a versatile tool that can be used for various text formatting tasks, making it an essential part of a Linux user's toolkit.

2026-05

Table of Contents#

  1. Basic Syntax of the pr Command
  2. Common Options and Their Usage
  3. Best Practices
  4. Example Usage
  5. Conclusion
  6. References

Basic Syntax of the pr Command#

The basic syntax of the pr command is as follows:

pr [options] [file(s)]
  • options: These are the various flags that can be used to customize the output.
  • file(s): The name(s) of the file(s) you want to format. If no file is specified, pr will read from the standard input.

Common Options and Their Usage#

Changing Page Length and Width#

  • -l or --length: This option is used to specify the number of lines per page. By default, the page length is 66 lines. For example, to set the page length to 50 lines, you can use the following command:
pr -l 50 file.txt
  • -w or --width: This option is used to specify the width of the page in characters. The default width is 72 characters. To set the page width to 80 characters, you can use the following command:
pr -w 80 file.txt

Adding Headers and Footers#

  • -h or --header: This option is used to add a custom header to each page. The header will be centered at the top of the page. For example, to add a header "My Document" to each page, you can use the following command:
pr -h "My Document" file.txt
  • -f or --form-feed: This option is used to add a form feed character at the end of each page. A form feed character is used to advance the printer to the next page. For example:
pr -f file.txt

Column Formatting#

  • - or --columns: This option is used to divide the output into multiple columns. For example, to divide the output into 3 columns, you can use the following command:
pr -3 file.txt

Best Practices#

  • Understand Your Requirements: Before using the pr command, clearly understand the formatting requirements. This will help you choose the appropriate options.
  • Test on a Small File: If you are using complex options, it is a good idea to test the command on a small file first to ensure that the output is as expected.
  • Use Descriptive Headers and Footers: Adding descriptive headers and footers can make the output more organized and easier to understand.

Example Usage#

Let's assume we have a text file named example.txt with the following content:

This is a sample text file.
It contains some sample sentences.
We will use this file to demonstrate the pr command.

Formatting with a Custom Header and Page Length#

pr -h "Sample Document" -l 30 example.txt

This command will add a header "Sample Document" to each page and set the page length to 30 lines.

Dividing the Output into Columns#

pr -2 example.txt

This command will divide the output of example.txt into two columns.

Conclusion#

The pr command in Linux is a powerful tool for formatting text files for printing. By using its various options, you can customize the output to meet your specific requirements. Whether you need to change the page length, add headers and footers, or divide the output into columns, the pr command has you covered.

References#