lp Command: Tutorial & Examples

Printing files from the shell

The lp command is a powerful utility in Linux that allows you to print files directly from the shell. While it may seem outdated to print files from the command line, it is actually an efficient way to manage printing tasks, especially when dealing with a large number of files or complex print settings.

The lp command sends a print job to a destination, typically a printer. It's a part of the Common Unix Printing System (CUPS), which is a modular printing system for Unix-like computer operating systems.

How does the lp command work?

The lp command works by sending a request to the CUPS server to print one or more files. The command can be customized with a variety of options to specify the destination printer, set the number of copies, choose sides for printing, and more.

What is the lp command used for?

The lp command is primarily used for:

  • Printing documents: Send files to the printer for physical output.
  • Managing print jobs: Control and monitor print jobs via additional commands.
  • Configuring print options: Customize settings such as paper size, orientation, and print quality.

Common command line parameters

The lp command includes several useful options:

  • -d <printer>: Specify the printer to send the job to.
  • -n <number>: Set the number of copies to print.
  • -o <option>: Set specific options (e.g., -o sides=two-sided-long-edge for double-sided printing).
  • -h: Suppress the print job's header.

Here are a few examples of using these options:

lp -d myprinter -n 3 myfile.txt

This command prints 3 copies of "myfile.txt" to the printer named "myprinter".

lp -o sides=two-sided-long-edge myfile.pdf

This command prints "myfile.pdf" double-sided.

Examples of the lp command

Let's look at some examples of how the lp command can be used:

lp myfile.txt

This command will print the file "myfile.txt" to the default printer.

lp -d myprinter myfile.txt

This command prints "myfile.txt" to the printer named "myprinter".

lp -n 5 myfile.txt

This command prints 5 copies of "myfile.txt" to the default printer.

The lp command typically outputs the request ID of the print job, which can be used to manage the print job with the lpstat or cancel commands. For example:

request id is myprinter-8 (1 file(s))

Common problems and pitfalls

A common issue that may occur when using the lp command is a network failure. If the CUPS server cannot reach the printer due to a network issue, the print job will fail. Troubleshooting may involve checking the network connection and ensuring that the printer is correctly configured in the CUPS server.

Another potential issue is the printer being paused. You can check the status and resume the printer using the lpstat command:

lpstat -p myprinter

This command shows the status of the printer. If it is paused, you can resume it with:

cupsenable myprinter

Tips and best practices

  • Check printer status: Always check the printer status before sending a job to avoid unnecessary errors.
  • Use print previews: If possible, use print preview options to minimize paper waste.
  • Batch printing: For multiple files, consider batching them together in a single command.

See also

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