How to use the command 'lpr' (with examples)
The ’lpr’ command is a CUPS tool for printing files. It allows users to send files to a printer for printing. This command is commonly used in Linux systems to manage printing tasks.
Use case 1: Print a file to the default printer
Code:
lpr path/to/file
Motivation: The motivation for using this example is to quickly print a file without specifying any additional settings. This is useful when the file needs to be printed using the default settings of the printer.
Explanation: The command ’lpr’ is followed by the path to the file that needs to be printed. This command will send the file to the default printer without any additional options.
Example output: The file will be sent to the default printer and will start printing according to the default settings of the printer.
Use case 2: Print 2 copies
Code:
lpr -# 2 path/to/file
Motivation: The motivation for using this example is to print multiple copies of a file without having to send multiple print jobs. This can save time and resources.
Explanation: The ‘-#’ option followed by a number specifies the number of copies to be printed. In this case, the value is 2, so two copies of the file will be printed.
Example output: The file will be sent to the printer and two copies of the file will be printed.
Use case 3: Print to a named printer
Code:
lpr -P printer path/to/file
Motivation: The motivation for using this example is to print a file to a specific printer when there are multiple printers available in the system.
Explanation: The ‘-P’ option followed by the printer name specifies the printer to which the file should be printed. In this case, the ‘printer’ is the name of the desired printer.
Example output: The file will be sent to the specified printer and will start printing according to the printer’s settings.
Use case 4: Print either a single page or a range of pages
Code:
lpr -o page-ranges=2|2-16 path/to/file
Motivation: The motivation for using this example is to print specific pages from a document instead of the entire document.
Explanation: The ‘-o page-ranges=’ option followed by either a single page number or a range of page numbers specifies the pages that need to be printed. In this case, ‘2’ represents a single page to be printed and ‘2-16’ represents a range of pages from 2 to 16.
Example output: The file will be sent to the printer and only the specified pages will be printed.
Use case 5: Print double-sided either in portrait or in landscape
Code:
lpr -o sides=two-sided-long-edge|two-sided-short-edge path/to/file
Motivation: The motivation for using this example is to print a document in double-sided mode, either in portrait or landscape orientation.
Explanation: The ‘-o sides=’ option followed by either ’two-sided-long-edge’ or ’two-sided-short-edge’ specifies the double-sided printing mode. ’two-sided-long-edge’ prints the document in portrait orientation with the flipping happening along the longer side of the paper, while ’two-sided-short-edge’ prints the document in landscape orientation with the flipping happening along the shorter side of the paper.
Example output: The file will be printed on both sides of the paper according to the specified orientation.
Use case 6: Set page size
Code:
lpr -o media=a4|letter|legal path/to/file
Motivation: The motivation for using this example is to specify a custom page size for printing the file.
Explanation: The ‘-o media=’ option followed by a page size value like ‘a4’, ’letter’, or ’legal’ specifies the desired page size for printing. In this case, ‘a4’ represents the A4 page size, ’letter’ represents the Letter page size, and ’legal’ represents the Legal page size.
Example output: The file will be printed using the specified page size.
Use case 7: Print multiple pages per sheet
Code:
lpr -o number-up=2|4|6|9|16 path/to/file
Motivation: The motivation for using this example is to print multiple pages of the document on a single sheet of paper.
Explanation: The ‘-o number-up=’ option followed by a number like ‘2’, ‘4’, ‘6’, ‘9’, or ‘16’ specifies the number of pages that should be printed on a single sheet of paper. In this case, ‘2’ represents printing two pages on a single sheet, ‘4’ represents printing four pages on a single sheet, and so on.
Example output: The file will be printed with the specified number of pages per sheet, reducing the number of pages needed for printing.
Conclusion
The ’lpr’ command is a versatile tool for sending files to printers in Linux systems. With various options available, it allows users to customize their printing tasks according to their specific requirements. Whether it’s printing multiple copies, selecting specific pages, or changing print settings, the ’lpr’ command provides flexibility and control for efficient printing.