lpr Command in Linux

Introduction

In this guide, you'll discover how to leverage the lpr command within Linux environments to print both text and PDF documents directly from your command line interface. The lpr command, a core component of the Common Unix Printing System (CUPS), empowers you to efficiently submit printing tasks to a designated printer.

We'll begin by exploring the fundamental usage of the lpr command, covering essential aspects like printer specification, copy number configuration, and various print setting adjustments. Subsequently, you'll gain hands-on experience in printing both simple text files and PDF documents utilizing the lpr command.

Understand the lpr Command

In this section, you'll gain a comprehensive understanding of the lpr command in Linux, a crucial tool for sending print jobs. As part of the Common Unix Printing System (CUPS), the lpr command allows systemadmin and users to print files directly from the command line.

To fully grasp the lpr command's functionality, let's consult its manual page:

$ man lpr

The manual page provides extensive details on the command's options and usage. Key takeaways include:

  • The lpr command, by default, sends files to the system's default printer.
  • The -P option enables you to specify a particular printer for the job.
  • The -# option controls the number of copies to be printed.
  • The -o option allows fine-tuning of print settings, such as paper size, page orientation, and duplex printing modes.

Let's illustrate with a simple example of printing a text file using lpr:

$ echo "Hello, World!" > hello.txt
$ lpr hello.txt

Example output:

Here, we first create a text file named hello.txt containing "Hello, World!". The lpr command then sends this file to the configured default printer for printing, streamlining tasks for any systemadmin.

Print a Text File Using the lpr Command

This section guides you through using the lpr command to print text files in your Linux environment.

Begin by creating a sample text file:

$ echo "This is a sample text file." > sample.txt

Now, use the lpr command to send sample.txt to your default printer:

$ lpr sample.txt

Example output:

This command instructs lpr to print the sample.txt file. If no printer is explicitly specified, it uses the system's default printer.

To print to a specific printer, use the -P option followed by the printer's name:

$ lpr -P printer_name sample.txt

Replace printer_name with the actual name of your target printer. Ensure proper printer setup for effective printing.

To print multiple copies, use the -# option followed by the number of copies:

$ lpr -## 3 sample.txt

This command will generate three copies of sample.txt, a valuable feature for systemadmin tasks and other repetitive printing needs.

Print a PDF File Using the lpr Command

In this section, you'll learn how to print PDF files using the lpr command on Linux.

First, obtain or create a sample PDF file. You can download one or create one using a PDF editor. For this example, we assume you have a PDF file named sample.pdf.

Use the lpr command to print the PDF:

$ lpr sample.pdf

Example output:

This command sends the sample.pdf file to the default printer. Ensure your printer supports PDF printing or CUPS is properly configured.

To specify a printer, use the -P option:

$ lpr -P printer_name sample.pdf

Replace printer_name with the desired printer's name.

To print multiple copies, use the -# option:

$ lpr -## 3 sample.pdf

This will print three copies of sample.pdf, useful for systemadmin tasks requiring multiple document copies.

Summary

In this lab, you gained practical experience with the lpr command in Linux, a command-line tool for managing print jobs. You learned the basics of the lpr command, including options for printer selection and copy control. You then practiced printing text files and PDF files, demonstrating how to use the -P option to target specific printers and the -# option to control the number of copies. This knowledge empowers you to effectively manage printing tasks as a systemadmin or Linux user.

400+ Linux Commands