pr - Linux


Overview

The pr command in Linux is used for paginating or formatting text files for printing. It converts text files for printing by adding headers, footers, and adjusting the text to fit the paper size. This utility is particularly useful for formatting source code or large log files for printing or reading.

Syntax

The basic syntax of the pr command is as follows:

pr [OPTION]... [FILE]...

If no FILE is specified, or if FILE is a dash (-), pr will read the standard input.

Options/Flags

  • -d, --double-space: Double space the output.
  • -h HEADER, --header=HEADER: Use HEADER instead of the filename in the page header.
  • -l NUMBER, --length=NUMBER: Set the page length in lines (default is 66 lines).
  • -o MARGIN, --indent=MARGIN: Set the size of the left margin to MARGIN spaces (default is 0).
  • -n, --number-lines: Number the lines of files, starting at 1.
  • -t, --omit-header: Omit the header and footer from the output.
  • -v, --show-nonprinting: Display non-printing characters (except tabs and line endings) and control characters visibly instead of as they are normally.
  • -w WIDTH, --width=WIDTH: Set the page width (the number of column positions available per line).

Examples

Example 1: Basic Usage
Print the content of file.txt with default settings:

pr file.txt

Example 2: Custom Header
Print file.txt with a custom header “Custom Header”:

pr -h "Custom Header" file.txt

Example 3: Line Numbers and Custom Page Width
Print file.txt, number all lines, with the width set to 100 characters:

pr -n -w 100 file.txt

Example 4: Combine Multiple Files
Paginate multiple files file1.txt and file2.txt with headers naming each file:

pr -m file1.txt file2.txt

Common Issues

  • File Not Found: Ensure that the file path is correct. Check for typos or use absolute path for clarity.
  • Improper Formatting: Adjust page length (-l) or width (-w) if the output is not formatted as expected.
  • Headers Clashing with Text: If headers are overlapping with text, consider using the -t option to remove headers or adjusting the page length.

Integration

Combine pr with lpr to format and print a file directly from the command line:

pr report.txt | lpr

Use pr in scripts to automatically format documents before sending them to a printer or converting them into PDFs:

pr -n report.txt > formatted_report.txt
  • lpr: Command to print files.
  • a2ps: Formats files for printing on a PostScript printer.
  • lp: Scheduling system print jobs.

For more information, you can explore the man pages:

man pr

or visit GNU Coreutils: pr for detailed documentation.