How to Paginate and Format Text Files in Linux Using the pr Command

Preparing Plain Text for Printing and Auditing in Linux

When generating automated system reports, reviewing long source code files, archiving batch logs, or preparing plain text documents for physical line printers in Linux, sending raw unformatted text directly to standard output or a printer daemon often produces unreadable results. Raw text lacks page demarcations, dates, filenames, and running headers. Linux incorporates a classic POSIX standard utility called pr (print), part of GNU Coreutils. The pr utility formats and paginates plain text files into clean, structured pages complete with custom headers, page numbering, timestamps, column layouts, and margin spacing.

Basic Syntax and Default Pagination Behaviour

The standard syntax for the pr utility is straightforward:

pr [options] [file...]

When you run pr on a text file without specifying additional options:

pr system_inventory.txt

The command parses the text and enforces standard printer page dimensions:

  • Page Length: Formats content into pages of exactly 66 lines (the historical standard for 11-inch continuous stationery at 6 lines per inch).
  • Running Header: Inserts a 5-line top header on every page displaying the file’s last modified timestamp, the filename, and the current page number (e.g. Page 1).
  • Trailing Margins: Inserts a 5-line blank footer at the bottom of each page to separate pages cleanly.

Configuring Custom Page Lengths with -l

If you are reviewing documents inside modern terminal windows or preparing output for letter-sized A4 paper with different line spacing, the 66-line default can be adjusted using the -l (page length) switch:

pr -l 50 server_audit.log

To view paginated output page by page inside your terminal without scrolling, pipe the result into less:

pr -l 40 audit.txt | less

Formatting Text into Multi-Column Layouts with -COLUMN

One of the most powerful features of pr is its ability to arrange single-column text into balanced, newspaper-style multi-column layouts:

pr -3 employee_roster.txt

The -3 parameter instructs pr to split the input into three evenly spaced parallel columns across the page. This is ideal for rendering long lists of usernames, hostnames, or IP addresses compactly without wasting horizontal screen space.

To merge multiple distinct files into side-by-side columns on the same page, use the -m (merge) switch:

pr -m -t file_a.txt file_b.txt

Customising Page Headers with -h

By default, pr prints the file path in the centre of the header. You can substitute this with a descriptive report title using the -h flag:

pr -h "Q4 Infrastructure Security Audit" -l 55 system_report.txt

If you are preparing files for automated ingestion where header bars would interfere with machine parsing, append the -t (omit header and trailer) flag to suppress all headers, footers, and page breaks entirely:

pr -t -2 names.txt

Adding Line Numbers and Margin Indentation

When reviewing code submissions or legal agreements, adding line numbering and left margin padding ensures easy cross-referencing:

  • Number Lines (-n): Prepends sequential line numbers to the text:
    pr -n script.py
    You can specify custom numbering widths and separator characters (e.g. pr -n:4 script.py numbers lines with 4 digits followed by a colon).
  • Margin Indentation (-o): Shifts the entire output to the right by a specified number of spaces to create a binding gutter for printed documents:
    pr -o 8 -n documentation.txt

Redirecting Formatted Output to Files and Print Daemons

Because pr outputs clean paginated text streams to standard output (stdout), it integrates seamlessly with file redirection and Linux printing tools:

Saving Paginated Documents to Disk

pr -h "Monthly Status Digest" -n -o 5 draft.txt > final_print_ready.txt

Sending Directly to the CUPS Print Daemon

pr -2 -h "Network Inventory" hosts.txt | lpr

Using pr allows Linux administrators to turn raw terminal data into publication-ready, numbered, and paginated documents without opening heavy graphical word processors.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.