Prepare Text Files for Printing with the Linux pr Command
Learn how to use Linux pr to paginate text files, customize headers, set page length, save formatted output, and send it to a print command.
The Linux pr command formats plain-text input into paginated, print-like output. It adds page structure and headers so a text file is easier to review or send to a printer.
pr formats the content; it does not communicate with a physical printer by itself. It writes the formatted result to standard output, normally the terminal. You can redirect that output to a file or pipe it to another command.
What the pr Command Does
The command's general syntax is:
pr [options] file
For example:
pr report.txt
This reads report.txt, formats it into pages, and displays the result on standard output. The original file is not modified.
Although the result may appear in a terminal, it is formatted as text for printing or for another print-like destination. Page boundaries can be represented with blank lines and form-feed characters, so terminal display may not look exactly like a sheet of paper from a printer.
pr can also accept more than one input file:
pr report.txt notes.txt
The files are processed as input and the combined formatted result is written to standard output.
Default Headers and Pagination
By default, pr divides output into pages and places a header at the top of each page. The header normally includes:
- The current date and time
- The input file name
- The page number
The date and time in the header are generated when pr runs. They are not necessarily the file's modification date.
Each page has a page length measured in lines. The selected page length includes the space needed for the header and the rest of the page layout, so changing the page length changes how much source text fits on each page.
When you run pr interactively, the result may scroll quickly or contain visible spacing that is difficult to interpret in a terminal. Saving the result to a file makes it easier to inspect the actual formatted text before printing.
Core pr Options for Basic Print Preparation
Customize the Header with -h
Use -h to provide replacement header text. This is useful when the file name is not an appropriate document title.
pr -h "Monthly Status Report" report.txt
The quoted text is used as the title portion of the header instead of the default file-name-based title. The quotes are important because the title contains spaces. Without quotes, the shell would treat each word as a separate argument.
Quoting also protects special characters from being interpreted by the shell. For example:
pr -h "Project Notes: Q3 Review" notes.txt
Choose single or double quotes according to the text you need to include. In basic shell commands, double quotes are convenient for titles containing spaces.
Set Page Length with -l
The -l option sets the page length as a numeric number of lines:
pr -l 66 report.txt
This prepares the file using 66 lines per output page. You may need a different value for different paper sizes, printer settings, margin requirements, or organizational formatting rules.
Page length is not the same as the number of source-text lines that appear on a page. Headers and page layout consume part of the available space. Always inspect the result when exact page breaks matter.
Combine pr Options
Options can be used together in one command. This example sets both a custom title and a 66-line page length:
pr -h "Monthly Status Report" -l 66 report.txt
The order of these options does not change their purpose. The command applies both settings to the formatted output.
A compact form may also be accepted by common pr implementations, but writing each option and its value separately is clearer for beginners:
pr -h "Custom Title" -l 66 report.txt
Save Formatted Output to a New File
Use shell redirection to save standard output. The > operator writes the formatted result to a new output file:
pr -h "Monthly Status Report" -l 66 report.txt > report-print.txt
This leaves report.txt unchanged and creates or replaces report-print.txt. Open the saved result with a text viewer before printing if you need to check headers and page breaks.
For example, you can inspect it with:
less report-print.txt
Be careful with the output filename. Redirection with > can overwrite an existing file.
Pipe Formatted Output to a Print Command
A pipe uses | to send one command's standard output to another command's standard input. If the system has a configured lpr printing client, you can submit the prepared output directly:
pr -h "Monthly Status Report" -l 66 report.txt | lpr
This formats the text first and passes the result to the print queue. The availability of lpr depends on the installed and configured printing service. Reviewing a saved file first is safer when page dimensions, headers, or page breaks are important.
Default and Customized Output Behavior
Complete Workflow Example
- Confirm that the source file exists and is readable.
- Format it with a useful title and page length.
- Save the result to a separate file.
- Review the saved output.
- Print it with a configured print command if the result is correct.
ls -l report.txt
pr -h "Monthly Status Report" -l 66 report.txt > report-print.txt
less report-print.txt
lpr report-print.txt
The final command is optional. If no printer service is available, keep the formatted file for later printing or transfer it to an appropriate system.
Troubleshooting pr
Input file cannot be opened
If pr reports that it cannot open the input file, the path may be wrong or you may not have read permission.
ls -l report.txt
pr ./report.txt
Verify the spelling, use the correct relative or absolute path, and check the file permissions. File ownership and permissions are covered in Manage File Ownership.
The custom header is split or unexpected
A title containing spaces must be quoted:
pr -h "Project Notes" notes.txt
Without quotes, the shell separates Project and Notes into different arguments.
Page breaks occur too early or too late
The selected page length may not match your desired layout. Try another numeric value with -l, then save and inspect the output:
pr -h "Project Notes" -l 60 notes.txt > notes-print.txt
Remember that headers and other page layout space reduce the amount of source text that fits on each page.
The source file appears unchanged
This is normal. pr reads the source and writes its formatted result to standard output; it does not edit the input file. Redirect the result to a different file or pipe it to a print command.
lpr is unavailable or printing fails
The lpr client or a configured printer service may be missing. Save the formatted output instead:
pr -h "Project Notes" -l 66 notes.txt > notes-print.txt
You can then install or configure the printing service for your Linux distribution, or move the prepared file to a system that can print it.
Exam-Relevant Notes
ulpr paginates and formats text; it does not itself send output to a physical printer.-h supplies custom header text.-l sets page length in lines.pr runs, not necessarily when the file was last changed.