lpr Command: Tutorial & Examples

Print files from the command line

The lpr command is a powerful utility in Linux used for printing files directly to a printer from the command line. It offers a way to interface with printers without the need for a graphical user interface (GUI), making it an essential tool for server management and automation tasks.

How lpr works

The lpr command interacts with the printing system of the Linux operating system. When you issue a print command using lpr, it sends the specified file to the default printer configured on the system. The command communicates with the print spooler, which manages print jobs and queues them for processing. The lpr command can handle various file formats and is capable of sending plain text files, PostScript files, and more.

What lpr does

The primary function of the lpr command is to send files to a printer. It can be used to print:

  • Text documents
  • PDF files
  • Image files
  • PostScript files

The command queues the job for printing and allows users to manage print jobs via other commands such as lpq and lprm.

What lpr is used for

lpr is commonly used for:

  • Printing documents from a terminal session
  • Automating print jobs in scripts
  • Managing print jobs on servers without a GUI
  • Printing batch jobs that require multiple documents to be printed in sequence

Why lpr is important

The lpr command is important for several reasons:

  • It provides a lightweight method for printing from the command line, which is essential for server environments.
  • It allows for automation of printing tasks, making it easier to integrate printing capabilities into scripts and workflows.
  • It is part of the Common Unix Printing System (CUPS), which is the standard printing system for Linux and Unix-like operating systems.

How to use lpr

Using the lpr command is straightforward. The basic syntax is:

lpr [options] [file...]

Here are some examples:

Print a single text file:

lpr document.txt

Print multiple files at once:

lpr document1.txt document2.txt

Print a PDF file:

lpr report.pdf

Print with a specific printer (if you have multiple printers):

lpr -P printer_name document.txt

Common command line parameters

lpr supports various options that can alter its behavior:

  • -P printer_name: Specify the printer to use.
  • -# num: Specify the number of copies to print.
  • -l: Print files in landscape mode.
  • -o option: Set print options, such as media=a4 to select paper size.
  • -h: Suppress the printing of the header.

Here are some examples of using options:

Print two copies of a document:

lpr -# 2 document.txt

Set media to A4 while printing:

lpr -o media=A4 document.txt

Potential problems and pitfalls

When using lpr, users may encounter several issues:

  • Printer not found: Ensure the printer is correctly configured in CUPS and is powered on.
  • Queue full: If the print queue is full, jobs may fail to print.
  • File format not supported: Ensure the file format is compatible with the printer being used.

To troubleshoot printer-related issues, check the printer status using:

lpstat -p

This command shows the status of printers configured on the system.

Common errors and troubleshooting

Some common errors that users may encounter include:

  • Error: Unable to connect to printer: This can occur if the printer is offline or not configured correctly. Verify the printer settings in CUPS using the web interface at http://localhost:631.

  • Error: No default printer: If no default printer is set, you can set one using:

    lpoptions -d printer_name
    
  • Job failed to print: Check the error logs for more details. You can view logs using:

    journalctl -u cups
    

Hacks and tricks

Here are some useful hacks and tricks for using lpr:

  • Print background jobs: Use lpr in scripts to print files generated by background jobs automatically.

  • Schedule print jobs: Combine lpr with the cron command to schedule print jobs at specific times.

  • Print to a PDF: Use lpr -o outputfile=filename.pdf to print a document as a PDF file instead of directly to a printer.

  • Check print job status: Use lpstat -o to see the status of all print jobs in the queue.

Tips and best practices

  • Always check printer configuration and status before sending print jobs.
  • Use -o options to customize print output according to your needs.
  • Keep the print spooler clean; regularly remove old jobs to avoid queue issues.
  • Monitor printer logs for errors and performance issues to ensure smooth printing operations.

Security considerations

When using lpr, it's crucial to consider the following security aspects:

  • Access Control: Ensure that only authorized users can send print jobs. Configure printer permissions in CUPS appropriately.
  • Data Privacy: Be cautious about printing sensitive documents. Use secure printing options if available.
  • Network Security: If using network printers, ensure that the network is secure to prevent unauthorized access.

Possible alternatives or related commands

While lpr is a robust command for printing, there are other related commands that may be useful:

  • lp: Similar to lpr, but with a slightly different syntax and options.
  • lpq: View the print queue for a specific printer.
  • lprm: Remove jobs from the print queue.
  • lpadmin: Configure printer settings in CUPS.

See also

The text above is licensed under CC BY-SA 4.0 CC BY SA