Introduction
In this lab, you will learn how to use the lpr command in Linux to print text and PDF files from the command line. The lpr command is part of the Common Unix Printing System (CUPS) and provides a way to send print jobs to a printer.
You will start by understanding the basic usage of the lpr command, including how to specify a printer, set the number of copies, and configure various print options. Then, you will practice printing a simple text file and a PDF file using the lpr command.
Understand the lpr Command
In this step, you will learn about the lpr command in Linux, which is used to send print jobs to a printer. The lpr command is a part of the Common Unix Printing System (CUPS) and is used to print files from the command line.
To understand the lpr command, let's start by checking its manual page:
$ man lpr
The manual page provides information about the various options and usage of the lpr command. Some of the key points are:
- The
lprcommand sends the specified files to the default printer for printing. - The
-Poption can be used to specify a particular printer to use. - The
-#option can be used to specify the number of copies to print. - The
-ooption can be used to set various print options, such as paper size, orientation, and duplex printing.
Now, let's try a simple example to print a text file using the lpr command:
$ echo "Hello, World!" > hello.txt
$ lpr hello.txt
Example output:
In this example, we first create a text file named hello.txt with the content "Hello, World!". Then, we use the lpr command to send the file to the default printer for printing.
Print a Text File Using the lpr Command
In this step, you will learn how to use the lpr command to print a text file.
First, let's create a sample text file to print:
$ echo "This is a sample text file." > sample.txt
Now, you can use the lpr command to send the file to the default printer for printing:
$ lpr sample.txt
Example output:
In this example, we use the lpr command to print the sample.txt file. The lpr command will send the file to the default printer configured on the system.
If you want to print the file to a specific printer, you can use the -P option to specify the printer name:
$ lpr -P printer_name sample.txt
Replace printer_name with the name of the printer you want to use.
You can also print multiple copies of the file by using the -# option:
$ lpr -## 3 sample.txt
This will print 3 copies of the sample.txt file.
Print a PDF File Using the lpr Command
In this step, you will learn how to use the lpr command to print a PDF file.
First, let's create a sample PDF file to print. You can download a PDF file from the internet or create one using a PDF editor. For this example, we'll use a sample PDF file named sample.pdf.
To print the PDF file, you can use the lpr command:
$ lpr sample.pdf
Example output:
In this example, we use the lpr command to print the sample.pdf file to the default printer.
If you want to print the PDF file to a specific printer, you can use the -P option to specify the printer name:
$ lpr -P printer_name sample.pdf
Replace printer_name with the name of the printer you want to use.
You can also print multiple copies of the PDF file by using the -# option:
$ lpr -## 3 sample.pdf
This will print 3 copies of the sample.pdf file.
Summary
In this lab, you learned about the lpr command in Linux, which is used to send print jobs to a printer. You started by understanding the lpr command, including its various options and usage. You then learned how to print a text file using the lpr command, including how to specify a particular printer and the number of copies to print. Finally, you explored printing a PDF file using the lpr command, which involves using the -o option to set print options.



