How to Use the Command 'lp' (with Examples)
The lp
command is a powerful utility in Unix-like operating systems, including Linux and macOS, that facilitates printing tasks. This command allows users to send files directly to a printer from the terminal, offering a variety of options to control print jobs effectively. With lp
, users can specify the printer, decide on the number of copies, choose specific pages to print, and even resume paused print jobs. Below, we’ll explore several practical use cases for the lp
command with detailed explanations and example outputs.
Use Case 1: Print the Output of a Command to the Default Printer
Code:
echo "test" | lp
Motivation:
Sometimes, users may want to quickly print a short text or the output of a command without saving it to a file first. Using lp
in combination with echo
, users can swiftly print text to paper, facilitating tasks like quick note-taking or debugging logs.
Explanation:
echo "test"
: This command writes “test” to standard output.|
: The pipe operator takes the output ofecho
and sends it as input to the following command.lp
: Sends the standard input to the default printer, printing the string “test”.
Example Output:
A printed page with the word “test” neatly printed on it.
Use Case 2: Print a File to the Default Printer
Code:
lp path/to/filename
Motivation:
Users often need to print documents stored on their local filesystems. This command helps users to easily print files by sending them directly to the default printer.
Explanation:
lp
: The command initiates the print process.path/to/filename
: Specifies the path to the file that you wish to print. The file is sent directly to the default printer for output.
Example Output:
A hardcopy of the file located at path/to/filename
is produced via the default printer.
Use Case 3: Print a File to a Named Printer
Code:
lp -d printer_name path/to/filename
Motivation:
In environments with multiple printers, users need to specify which printer should handle the print job. This is crucial in offices or labs where various printers exist for different purposes, such as high-quality printing or color printing.
Explanation:
lp -d
: The-d
flag allows the user to specify a printer other than the default.printer_name
: Identifies the specific printer to which the file will be sent.path/to/filename
: The file you wish to print is specified here.
Example Output:
The specified printer (printer_name
) prints the contents of path/to/filename
.
Use Case 4: Print N Copies of a File to the Default Printer
Code:
lp -n N path/to/filename
Motivation:
When preparing for meetings or distributing materials, users often require multiple copies of a document. This command helps in easily specifying the number of copies to be printed.
Explanation:
lp -n
: The-n
option allows users to define the number of copies required.N
: Replace this with the desired number of copies.path/to/filename
: The path to the file you want to print multiple copies of.
Example Output:
N printed copies of path/to/filename
are produced by the default printer.
Use Case 5: Print Only Certain Pages to the Default Printer
Code:
lp -P 1,3-5,16 path/to/filename
Motivation:
Users frequently need to print specific sections of a document, such as particular chapters, sections, or pages with critical information. This saves paper and focuses on necessary content only.
Explanation:
lp -P
: The-P
parameter enables page selection for printing.1,3-5,16
: This specifies pages to print. Here, pages 1, 3 through 5, and 16 are printed.path/to/filename
: Indicates the document from which the specified pages will be printed.
Example Output:
Printed pages include only pages 1, 3, 4, 5, and 16 from path/to/filename
.
Use Case 6: Resume Printing a Job
Code:
lp -i job_id -H resume
Motivation:
Printing jobs may be halted for various reasons, such as printer unavailability or error conditions. Users need to resume such jobs when the conditions return to normal. This command facilitates the seamless resumption of interrupted printing tasks.
Explanation:
lp -i
: The-i
flag specifies the job ID of the print task you wish to control.job_id
: Represents the identifier of the stalled print job you want to resume.-H resume
: The-H resume
option tells the system to restart the queued job.
Example Output:
The paused print job with the given job_id
resumes printing from its last state.
Conclusion:
The lp
command is a versatile tool that aids in efficient and flexible handling of print jobs from the command line. Whether you need to swiftly print the output of a command, send documents to a specific printer, control the number of copies, or select specific pages, lp
provides the options to customize your printing tasks to suit your specific needs. Each of these use cases illustrates the command’s potential in simplifying and automating aspects of document management in a computer environment.