
pr Command in Linux
The pr command in Linux converts a text file for printing. It prepares files for line printers, paginating and adding headers, footers, and other formatting as specified by options.
The pr command provides several features that make printing text files more structured, organized, and suitable for formal presentation.
Table of Contents
Here is a comprehensive guide to the options available with the pr command in Linux −
Syntax of pr Command
The syntax of the Linux pr command is as follows −
pr [options...] [file...]
In the above syntax, the [options...] field is used to specify one or more options to modify the output. The [file...] refers to one or more files to be processed. If no file is provided, or if the file is specified as -, pr reads input from the standard input.
pr Command Options
The options for the pr command are listed below −
Flag | Option | Description |
---|---|---|
-COLUMN | --columns=COLUMN | Output in COLUMN columns, printed down unless -a is used; balances line numbers across columns |
-a | --across | Print columns across instead of down, used with -COLUMN |
-c | --show-control-chars | Use hat notation (e.g., ^G) and octal backslash notation for control characters. |
-d | --double-space | Double space the output |
-D | --date-format=FORMAT | Use FORMAT for the header date |
-e[CHAR[WIDTH]] | --expand-tabs[=CHAR[WIDTH]] | Expand CHARs (tabs) to a width (default 8) |
-F, -f | --form-feed | Use form feeds instead of newlines to separate pages, with different header/trailer styles |
-h HEADER | --header=HEADER | Use HEADER in page headers instead of filenames; -h "" suppresses the header |
-i[CHAR[WIDTH]] | --output-tabs[=CHAR[WIDTH]] | Replace spaces with CHARs (tabs) to a width (default 8) |
-J | --join-lines | Merge full lines without truncation or column alignment; --sep-string sets separators |
-l PAGE_LENGTH | --length=PAGE_LENGTH | Set page length to PAGE_LENGTH lines (default 66); implies -t if PAGE_LENGTH <= 10 |
-m | --merge | Print files in parallel, one per column; truncate lines unless -J is used |
-n[SEP[DIGITS]] | --number-lines[=SEP[DIGITS]] | Number lines with DIGITS digits and separator SEP; starts from the first line |
-N NUMBER | --first-line-number=NUMBER | Start line numbering from NUMBER on the first page printed |
-o MARGIN | --indent=MARGIN | Offset each line by MARGIN spaces, added to page width without affecting -w or -W |
-r | --no-file-warnings | Suppress warnings when files cannot be opened |
-s[CHAR] | --separator[=CHAR] | Separate columns with CHAR (default <TAB>); disables line truncation except with -w |
-S[STRING] | --sep-string[=STRING] | Separate columns with STRING; default separator is <TAB> with -J, space otherwise |
-t | --omit-header | Omit page headers and trailers; implied if PAGE_LENGTH <= 10 |
-T | --omit-pagination | Omit headers, trailers, and input pagination by form feeds |
-v | --show-nonprinting | Use octal backslash notation for non-printing characters |
-w PAGE_WIDTH | --width=PAGE_WIDTH | Set page width to PAGE_WIDTH characters (default 72); applies to multi-column output |
-W PAGE_WIDTH | --page-width=PAGE_WIDTH | Set page width to PAGE_WIDTH (default 72); truncates lines unless -J is used |
--help | Display help information and exit | |
--version | Output version information and exit |
Examples of pr Command in Linux
This section demonstrates the usage of pr command in Linux with examples −
Converting a Text File for Printing with Default Settings
To prepare a text file for printing with default settings, use the pr command with the file name −
pr file.txt

The above command formats file.txt for printing, adding page numbers, and a basic header.
Adding Custom Header
To add a custom header, use the -h or --header option with the header string −
pr -h "My Document" file.txt

Setting Custom Page Length
To set the custom page length, use the -l or --length option with a length integer. For example, to set the page length to 30 lines use the following command −
pr -l 30 file.txt
The default page length is 66 lines.
Merging Files Side by Side
To merge files side by side, use the -m or --merge option with the file names −
pr -m file1.txt file2.txt

Adding Double Space Between Lines
To add double space between lines in the output, use the -d or --double option with the pr command −
pr -d file.txt

Starting Print from a Specific Page
To start printing from a specific page, use the +N option. The N is the page number. For example, to print a page from page 4 to the end of the file, use the +4 with pr command in the following way −
pr +4 file.txt
To specify the range of page, use the pr command in the following manner −
pr +4:7 file.txt
The 7 in the above command signifies the ending page.
Note that if FIRST_PAGE exceeds the total number of pages, the output will be empty; if LAST_PAGE exceeds the total number of pages, the output will end at the last available page.
Adding Line Number
To add a line number to the output, use the -n or --number-lines option −
pr -n file.txt

The above command adds a default separator (tab) after the line number.
To specify the custom separator, use the -n option with the separator. For example, to set a colon (:) as a separator, use the following command −
pr -n: file.txt
Changing Page Width for Multi-Column Output
To change the page width for a multi-column layout, use the -w or --width option. The default width of a column is 72 characters, to set it to 80, use the pr command in the following way −
pr -w 80 file.txt
Skipping Header and Trailers from Output
To omit the header and footer from the output, use the -t or --omit-header option −
pr -t file.txt
Setting Custom Date Format
To modify the date format in the output header, use the -D or --date-format option −
pr -D "%Y:%m:%d" file.txt

Displaying Usage Help
To print the pr command usage help, use the --help option −
pr --help
Conclusion
The pr command in Linux is a handy tool for formatting text files for printing, offering features such as pagination, custom headers, line numbering, and multi-column layout. With its extensive functionality, pr is essential for structuring text files for line printers or formal documentation purposes.