How to Use the Linux pr Command to Format Text Files for Printing

The Linux terminal is exceptional at processing raw data, but it is notoriously poor at presenting that data in a human-readable, physical format. If you send a standard bash script or a massive system log directly to a physical printer using the lp command, it will print as a continuous, unformatted block of text spanning across the page perforations with zero margins or context. To solve this, Linux includes the pr (print) command. This utility intercepts raw text and paginates it, adding headers, margins, and page numbers before the file ever hits the printer.

Basic Pagination with the pr Command

The pr command does not actually communicate with your physical printer. It is a formatting tool. It takes an input file, structures it into visual “pages” with headers, and outputs the formatted text to your screen, which you can then redirect to a printer.

  1. Open your terminal application.
  2. Assume you have a long script named server_audit.sh. To see how pr formats it, type:

pr server_audit.sh

Press Enter. The output will flash across your screen. You will notice that pr has automatically added a five-line header to the top of the file containing the current date, the exact time, the filename, and “Page 1”. It also automatically inserts 56 lines of text per page, followed by a footer margin, ensuring perfect alignment on standard A4 or Letter paper.

Printing in Multiple Columns

If you are printing a long, narrow list (such as a list of usernames or IP addresses), printing one item per line wastes massive amounts of expensive paper. The pr command allows you to instantly reformat a single-column list into a multi-column newspaper layout.

To format a file named ip_list.txt into three even columns, use the column flag (e.g., -3):

pr -3 ip_list.txt

The utility will mathematically divide the text and arrange it perfectly across the page horizontally, drastically reducing your page count.

Customising the Header

By default, pr uses the filename as the bold header at the top of every page. If you are piping output directly from a command rather than a file, the header will display blank or show temporary buffer names. You can override this and inject a custom title using the -h (header) flag.

For example, if you want to print a list of running processes and title the document “Daily Resource Audit,” you can pipe the ps command directly into pr:

ps aux | pr -h "Daily Resource Audit"

Every page generated will now boldly display “Daily Resource Audit” alongside the timestamp and page numbers.

Sending the Formatted Text to the Printer

Once you are satisfied with how the pr command has formatted your text on the screen, you can pipe the final output directly into the lp (line printer) command to send it to your default physical printer.

pr -3 -h "Employee Directory" users.txt | lp

The printer will spool up and produce a perfectly paginated, multi-column document with professional headers, proving that you do not need a graphical word processor to create clean, readable reports.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

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