VMware ESXi and vSphere Cluster Management
Prepare Text Files for Printing with the Linux pr Command
Learn how to use Linux pr to paginate plain-text files, customize headers, set page length, review output, and save print-ready formatting.
What the Linux pr Command Does
pr is a Unix/Linux utility that paginates and formats text for print-style output. It adds page-oriented structure to a plain-text file, including a header and page numbering.
By default, pr writes its formatted result to standard output. Standard output is the normal stream where a command displays its result, so you will usually see the formatted text in the terminal unless you redirect it to another file.
Basic Syntax
The basic form is:
pr [options] file
For example, format a single text file using the default layout:
pr notes.txt
This displays paginated output in the terminal. The original notes.txt file is not modified.
To save the formatted result, use shell redirection. Redirection sends command output to a file instead of displaying it directly:
pr notes.txt > notes-print.txt
The source file remains unchanged, while notes-print.txt contains the generated print-oriented formatting.
Understanding the Default Page Format
When run without formatting options, pr divides the input into pages and adds a header at the top of each generated page. A header is identifying information printed above the page content.
The standard header typically contains:
| Component | What It Identifies |
|---|---|
| Date and time | When the formatted output was generated |
| Input filename | The source file being formatted |
| Page number | The sequential number of the current formatted page |
The exact spacing and presentation can vary between Unix/Linux implementations, but the purpose is the same: identify each page and its position in the document.
A page number is the sequential number assigned to each formatted page. As pr reaches the configured page length, it inserts a page break and starts the next page with a new header and page number.
Customizing the Header with -h
Use the -h option to supply replacement header text. This lets you identify the document with a title instead of relying on the input filename portion of the standard header.
pr -h "Weekly Operations Notes" notes.txt
The quotation marks are important because the title contains spaces. Without quotes, the shell would treat each word as a separate command argument.
Single quotes work as well:
pr -h 'Weekly Operations Notes' notes.txt
Use a custom title when the filename is abbreviated, technical, or not suitable for a printed document.
Setting Page Length with -l
The -l option accepts a line count and sets the page length used by pr:
pr -l 40 notes.txt
This formats the file using 40 lines per page, causing page breaks to occur sooner than with a longer page length. Headers and the surrounding page structure are part of the generated output, so review the result rather than assuming that every source line will appear in the same physical position.
For example, if a document must be divided into short pages for review, combine a custom title with a 40-line page length:
pr -h "Weekly Operations Notes" -l 40 notes.txt
The numeric value should reflect the layout you intend to use. A physical printer, paper size, font, and margins can affect how the text ultimately appears on paper.
Core pr Options
| Option | Argument | Purpose | Example |
|---|---|---|---|
-h | Header text | Supplies custom text for the page header. | pr -h "Document Title" file.txt |
-l | Line count | Sets the number of lines used for each formatted page. | pr -l 50 file.txt |
Save and Review the Formatted Output
It is good practice to inspect the result before printing. Display a small or ordinary file directly:
pr notes.txt
For a longer document, save the result so you can review it without repeatedly running the command:
pr -h "Document Title" -l 50 file.txt > formatted-file.txt
This command combines a custom header and a 50-line page length, then saves the result in a separate file. The input file is left unchanged.
You can examine the saved file with a terminal viewer such as less, or inspect it with another text-reading command. Look for:
- Headers appearing where expected
- Page numbers progressing correctly
- Page breaks occurring at suitable locations
- Long lines that may exceed the intended printer width
- Unexpected blank lines or source-file formatting
Terminal display width and physical printer layout are not identical. A line that appears to fit in a terminal may wrap on paper if the printer uses a different font, paper size, or margin setting.
Formatting Is Separate from Printing
After reviewing the generated file, you can pass it to a separate system printing workflow. For example, a system may provide a print command such as lp or lpr, depending on its configuration:
lp formatted-file.txt
The exact printer command and available options depend on the Linux system. You can also print the redirected file later, which is useful when you need to verify or archive the formatted text before submitting it.
The key distinction is:
prtransforms plain text into paginated, print-oriented output.- A printer command submits a file or stream to a configured printer.
Troubleshooting
The source file does not appear to change
This is expected. pr normally writes transformed text to standard output and does not edit the input file.
pr file.txt > formatted-file.txt
Redirect the output to a new file when you need a saved formatted version. Avoid redirecting directly to the same file you are reading, because that can overwrite the source before the command has finished reading it.
The custom header is split into separate arguments
The likely cause is missing shell quotes around header text containing spaces. Quote the complete title:
pr -h "Quarterly Project Review" file.txt
Page breaks occur too early or too late
The configured line count may not match the intended layout. Adjust the value passed to -l, then review the result:
pr -l 40 file.txt > review.txt
pr -l 50 file.txt > review.txt
Choose the value that best matches the paper size, margins, font, and header space used by the final printing workflow.
The document is formatted but not physically printed
pr only prepares the content. Save or pipe its output to an appropriate printing workflow after checking the layout:
pr -h "Document Title" -l 50 file.txt > formatted-file.txt
Then submit formatted-file.txt using the printer command configured on your system.
Practical Command Summary
# Apply the default print-oriented formatting
pr file.txt
# Set custom header text
pr -h "Document Title" file.txt
# Set each generated page to 50 lines
pr -l 50 file.txt
# Combine a custom header and page length, then save the output
pr -h "Document Title" -l 50 file.txt > formatted-file.txt
Exam-Relevant Notes
prpaginates and formats text for print-style output.- Standard output is where
prsends its formatted result by default. - Redirection with
>saves that result in another file. -hsupplies custom header text, and titles containing spaces must be quoted.-lsets page length in lines and affects where page breaks occur.- The input file is not modified by an ordinary
prcommand. - Formatting output and submitting a print job are separate operations.
For a focused workflow, start with preparing a file for printing, inspect the generated text, adjust the header or page length, and only then send the reviewed output to a printer.