lpr Command in Linux



lpr is a basic command used in Linux to send documents or files to a printer. It stands for "line printer" and allows you to print files without having to open them in a viewer or text editor. This command is pretty useful in case you want to automate print tasks and integrate printing into scripts or batch processes.

Table of Contents

Here is a comprehensive guide to the options available with the lpr command −

Syntax of lpr Command

To use the lpr command, the syntax is quite simple, which is given below −

lpr [options] [files]

Here,

  • [options] are the flags that customize the command's behavior.
  • [files] are the documents you want to print.

lpr Command Options

The Linux lpr command offers several options to customize your printing tasks. Here's a table summarizing some of the key options −

Options Description
-# num Set the number of copies to print.
-E Encrypt the connection to the print server.
-H server [:port] Specify the print server and optional port to connect to.
-m Send an email upon job completion.
-o option Provides printer-specific options. For example: jobsheet=standard includes a job sheet, media=size sets the paper size, and number-up=N prints multiple pages per sheet.
-P printer Specify the printer to which the file should be sent.
-q Hold the job in the queue, preventing it from printing immediately.
-r Remove the file from the system after printing.
-T title Set a specific title for the print job.
-U username Specify the username for the print job.

Examples of lpr Command in Linux

Below are some practical scenarios where the command lpr can be utilized in Linux.

  • Basic Usage
  • Choosing a Specific Printer
  • Printing Several Copies
  • Deleting File After Printing
  • Setting a Job Title

Basic Usage

Imagine you have a file named document.txt that you need to print. The basic lpr command for this task would be −

lpr document.txt

This command sends the file document.txt to the default printer. It tells the printer to print the document using its standard settings.

Choosing a Specific Printer

If you want to print a file on a specific printer, you can use the -P option. For example, let's say you want to print document.txt on a printer named office_printer, you would use −

lpr -P office_printer document.txt

This command directs the document to the printer named office_printer, and ensures it doesn't go to the default printer but to the one you specified.

Printing Several Copies

When you need multiple copies of a document, you can specify the number of copies using the -# option. For instance, to print three copies of document.txt, you would run −

lpr -# 3 document.txt

This command will print three copies of document.txt. It's useful if you need to distribute multiple copies of the same document.

Deleting File After Printing

If you want the file to be automatically deleted from the system after it has been printed, you can use the -r option. For instance −

lpr -r document.txt

This command sends document.txt to the printer and then removes it from your computer. This is helpful for keeping your files organized and saving space.

Setting a Job Title

To set a custom title for your print job, you can use the -T option. This title can help you identify your print job in a busy printer queue. For example −

lpr -T "My Report" document.txt

This command sends document.txt to the printer with the title "My Report". It's useful for easily recognizing your print job among others.

Conclusion

The lpr command is a vital tool for Linux users who need to send print jobs directly from the command line. Whether you're printing single documents, multiple copies, or integrating printing into scripts, lpr offers the flexibility and functionality needed to handle various printing tasks.

This tutorial covered the installation, syntax, options, and practical examples of using the command lpr. Mastering this command will enhance your efficiency in managing print jobs and streamline your workflow in a Linux environment.

Advertisements