pr

text processinglinux
The pr command is one of the most frequently used commands in Linux/Unix-like operating systems. pr The pr command formats text files for printing. It can paginate text with headers and footers, create multi-column output, merge files side by side, and adjust various formatting elements like margins and page length.

Quick Reference

Command Name:

pr

Category:

text processing

Platform:

linux

Basic Usage:

pr [options] [arguments]

Common Use Cases

  • 1

    Text pagination

    Format text data for printing

  • 2

    Report generation

    Generate reports with page breaks and headers

  • 3

    Scripting

    Use in shell scripts to format text data programmatically

  • 4

    Text processing

    Manipulate text data in pipelines and scripts

Syntax

pr [OPTION]... [FILE]...

Options

Option Description
+FIRST_PAGE[:LAST_PAGE] Begin printing with page FIRST_PAGE and stop with LAST_PAGE
-COLUMN Output COLUMN columns and print columns down
-a Print columns across rather than down (used with -COLUMN)
-d Double space the output
-h HEADER Use a centered HEADER instead of filename in page header
-l PAGE_LENGTH Set the page length to PAGE_LENGTH (66) lines
-m Print all files in parallel, one in each column
-n Number lines
-o MARGIN Offset each line with a margin MARGIN spaces wide
-t Omit page headers and trailers
-w PAGE_WIDTH Set page width to PAGE_WIDTH (72) characters

Examples

How to Use These Examples

The examples below show common ways to use the pr command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

pr filename.txt

Formats the file with default settings, adding a header (date, time, filename, and page number) and pagination. Each page is 66 lines by default, with headers and blank lines.

pr -h "Custom Header" filename.txt

Format the file with a custom header instead of the default filename. This is useful when you want more descriptive page headers.

pr -l 50 filename.txt

Sets the page length to 50 lines instead of the default 66. This helps adjust the output to different paper sizes or printing requirements.

pr -2 filename.txt

Format the output in 2 columns. The text is split across columns from top to bottom, which is useful for creating more compact printouts.

Advanced Examples:

pr -m file1.txt file2.txt

Merge two files side-by-side with a separator between them. This is useful for comparing two text files or creating parallel presentations of content.

pr -n -h "Source Code" sourcefile.c

Number all lines and add a custom header. This is helpful when printing source code for review or reference.

pr +3:7 filename.txt

Print only pages 3 through 7. This selective printing is useful when you only need specific sections of a longer document.

pr -t -o 10 filename.txt

Omit the header and trailing blank lines (-t) and offset each line by 10 characters (-o 10). This creates a margin on the left side without headers or footers.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

What PR Does:

  • Formats text files for printing with headers, footers, and page numbers
  • Creates multi-column output from a single file or merges multiple files side-by-side
  • Controls page length, width, and margins
  • Numbers lines and/or pages
  • Handles tab expansion and space to tab conversion
  • Manages page breaks and form feeds
  • Provides options to customize headers and footers

Common Use Cases:

  • Preparing text files for clean, professional-looking printouts
  • Creating multi-column documentation or listings
  • Comparing two or more text files side-by-side
  • Numbering lines in source code for reference
  • Formatting log files with proper pagination and headers
  • Setting custom margins and page dimensions for special printing needs

Page Layout:

  • By default, a page is 66 lines long (typical for US Letter/A4 paper at 6 lines per inch)
  • Standard layout includes 5 lines for header, 5 lines for footer, and 56 lines for content
  • Default page width is 72 characters (common terminal width)
  • Headers include date, time, filename, and page number unless customized
  • Form feeds (^L) in the input file force a page break

Related Commands:

  • cat - Concatenate files without special formatting
  • fmt - Simple text formatter that can wrap and adjust text
  • expand - Convert tabs to spaces
  • unexpand - Convert spaces to tabs
  • fold - Wrap each line to a specified width
  • nl - Number lines of files
  • lpr, lp - Send files to a printer

Tips & Tricks

1

Use the -h header option to specify a header for each page

2

Use the -w width option to set the page width

3

Use the -l length option to set the page length

4

Use the -n option to suppress page headers and footers

5

Use the -d option to double-space the output

Common Use Cases

Text pagination

Format text data for printing

Report generation

Generate reports with page breaks and headers

Scripting

Use in shell scripts to format text data programmatically

Text processing

Manipulate text data in pipelines and scripts

Data presentation

Present data in a structured and readable format

Related Commands

These commands are frequently used alongside pr or serve similar purposes:

Use Cases

1

Text pagination

Format text data for printing

2

Report generation

Generate reports with page breaks and headers

3

Scripting

Use in shell scripts to format text data programmatically

4

Text processing

Manipulate text data in pipelines and scripts

5

Data presentation

Present data in a structured and readable format

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the pr command works in different scenarios.

$ pr
View All Commands