How to use the command 'lpr' (with examples)
The lpr
command, part of the Common UNIX Printing System (CUPS), is used for printing files from the command line. It simplifies the process of sending print jobs to printers by offering various options to modify and control print jobs. With lpr
, users can print directly to default or specified printers, manage print settings such as copies, page size, and orientation, and handle complex print tasks efficiently.
Use case 1: Print a file to the default printer
Code:
lpr path/to/file
Motivation:
When you simply need to print a document quickly and are satisfied with the default printer settings, using this command saves time. It’s particularly useful when working remotely on a server where GUI access may be limited, allowing files to be printed directly without manual intervention.
Explanation:
lpr
: The command used to send the file to the printer.path/to/file
: Specifies the file you want to print. The path can be absolute or relative, depending on where the file is located.
Example Output:
You would not see any print dialog or additional output. The file will be queued and then printed on the default printer.
Use case 2: Print 2 copies
Code:
lpr -# 2 path/to/file
Motivation:
There are cases where multiple hard copies of a document are required, such as when printing meeting materials, forms, or handouts. This command streamlines the process by specifying the number of copies directly from the command line.
Explanation:
-#
: This option tellslpr
how many copies of the document should be printed.2
: The number after-#
specifies that two copies of the document should be printed.path/to/file
: The file to be printed multiple times.
Example Output:
The printer will output two identical copies of the designated document, ensuring you have the hard copies you need.
Use case 3: Print to a named printer
Code:
lpr -P printer path/to/file
Motivation:
In environments with multiple printers, such as office networks, specifying a particular printer is crucial to ensure the document reaches the correct location. This is especially useful in large offices where centralized printing is managed over several floors or areas.
Explanation:
-P
: This flag allows you to specify the printer you want to use for the job.printer
: The name of the target printer. This name is the one given by the network or system when the printer is installed.path/to/file
: The document you wish to print.
Example Output:
The document bypasses other queues and is processed by the specified printer, providing flexibility in managing print resources.
Use case 4: Print either a single page (e.g. 2) or a range of pages (e.g. 2–16)
Code:
lpr -o page-ranges=2|2-16 path/to/file
Motivation:
Selective printing of specific pages from a larger document is often necessary to conserve resources or focus on certain sections, such as printing just the summary or a specific chapter of a report.
Explanation:
-o
: Stands for option, which provides various customizable features for the print job.page-ranges=2|2-16
: This specific option lets you define which part of the document to print.2
prints only the second page, while2-16
prints from page 2 to page 16.path/to/file
: The file from which the specified pages will be printed.
Example Output:
Only the pages specified in the range are printed, reducing unnecessary print jobs and conserving paper.
Use case 5: Print double-sided either in portrait (long) or in landscape (short)
Code:
lpr -o sides=two-sided-long-edge|two-sided-short-edge path/to/file
Motivation:
Double-sided printing is eco-friendly and can save paper. Choosing the edge for flipping allows for either a booklet-style (landscape) or standard document style (portrait) output, depending on the document’s intended use or format.
Explanation:
-o
: Modifies the print job settings.sides=two-sided-long-edge
: Prints on both sides of the paper, flipping on the long edge for portrait orientation.sides=two-sided-short-edge
: Prints on both sides of the paper, flipping on the short edge for landscape orientation.path/to/file
: The document to be printed in duplex format.
Example Output:
The pages are printed on both sides, with a layout that matches the chosen edge, providing a professional and resource-efficient output.
Use case 6: Set page size
Code:
lpr -o media=a4|letter|legal path/to/file
Motivation:
Different documents require different paper sizes; for example, official letters or legal documents often have specific size requirements. Adjusting the size ensures the document is formatted correctly and professionally for its intended use.
Explanation:
-o
: Provides options to customize printing.media=a4|letter|legal
: Sets the size for the printed document. A4 is common in most countries, letter size is popular in the US, and legal size is used for official legal documents.path/to/file
: The file you intend to print in a specified size.
Example Output:
The file is printed on the chosen paper size, ensuring it fits the required format and looks professional.
Use case 7: Print multiple pages per sheet
Code:
lpr -o number-up=2|4|6|9|16 path/to/file
Motivation:
When conserving paper or creating compact summaries is essential, such as printing handouts or study materials, fitting multiple pages on a single sheet provides a practical solution.
Explanation:
-o
: Customizes the print job options.number-up=2|4|6|9|16
: Determines how many pages will be printed on a single physical sheet of paper, reducing the amount of paper used.path/to/file
: The file from which these multiple pages will be sourced.
Example Output:
Sheets are printed with the specified number of pages per sheet, allowing consolidation of content and reduction of paper use.
Conclusion:
The lpr
command offers flexible and powerful options for printing in Unix-based systems, enabling users to tailor their print jobs to meet specific needs. Whether managing multiple printers, conserving resources, or handling large documents, lpr
provides easy-to-use commands that deliver professional results with minimal effort.