How to use the command lp (with examples)
The lp
command is used to print files. It allows you to send files to a printer for printing. This command is especially useful when you want to print files from the command line without having to open a graphical interface.
Use case 1: Print the output of a command to the default printer
Code:
echo "test" | lp
Motivation: Sometimes you may want to print the output of a command directly to a printer. This is useful when you want a physical copy of the output.
Explanation: The command echo "test"
prints the string “test”. The output of this command is then piped to the lp
command, which sends it to the default printer.
Example output: The string “test” will be printed by the default printer.
Use case 2: Print a file to the default printer
Code:
lp path/to/filename
Motivation: When you have a file that you want to print, you can use this command to send it to the default printer without having to open the file in an application.
Explanation: The lp
command takes the path to a file as an argument and sends it to the default printer for printing.
Example output: The file path/to/filename
will be printed by the default printer.
Use case 3: Print a file to a named printer
Code:
lp -d printer_name path/to/filename
Motivation: In some cases, you may have multiple printers available and want to specify which printer should be used for printing a file.
Explanation: The -d
option is used to specify the name of the printer. The printer_name
argument should be replaced with the actual name of the desired printer. The path to the file to be printed is also provided as an argument.
Example output: The file path/to/filename
will be printed by the printer with the name printer_name
.
Use case 4: Print N copies of a file to the default printer
Code:
lp -n N path/to/filename
Motivation: There may be times when you need multiple copies of a file. Instead of manually printing each copy, you can use this command to specify the number of copies you want.
Explanation: The -n
option is used to specify the number of copies. Replace N
with the desired number of copies. The path to the file is provided as an argument.
Example output: N copies of the file path/to/filename
will be printed 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: If a file has multiple pages and you only need to print specific pages, this command allows you to specify the pages to be printed.
Explanation: The -P
option is used to specify the pages to be printed. In the example above, pages 1, 3-5, and 16 will be printed. The path to the file is provided as an argument.
Example output: Only pages 1, 3-5, and 16 of the file path/to/filename
will be printed by the default printer.
Use case 6: Resume printing a job
Code:
lp -i job_id -H resume
Motivation: In case a print job gets interrupted or paused, you can use this command to resume printing the job without having to start the print job from the beginning.
Explanation: The -i
option is used to specify the job ID of the print job that needs to be resumed. The job_id
argument should be replaced with the actual job ID. The -H resume
option is used to resume the specified print job.
Example output: The print job with the given job ID will be resumed and continue printing.
Conclusion:
The lp
command is a versatile tool for printing files from the command line. It provides various options to customize the printing process, such as specifying the printer, number of copies, and even specific pages to print. With the examples provided, you should be able to use the lp
command effectively to print files.