Key Takeaways
- Printing from the command line with lp can be useful for sending files directly to the printer without opening an application.
- Most distributions include lp, but you may need to install it using commands like “sudo apt install cups”.
- Using lpstat, lpoptions, and lpinfo can help you find information about your printer and send print jobs from the command line with layout options.
The Linux lp command lets you print from a terminal window. You can send print jobs manually or from within scripts, set page orientation and other preferences, and list your printer’s capabilities.
Why Print From the Command Line?
Using nothing more than my superpower of assumption, I think it’s likely most printing from Linux computers in domestic settings is done from inside a GUI application.
Of course, there are text-based user interface (TUI) applications that support printing, too. These run inside a terminal window, and allow you to send content to your printer. But still, I expect the majority of printing comes from inside GUI applications.
Printing text files from the command line, outside any application, isn’t an everyday activity for most people, but it’s something that can be useful. You don’t need to launch a program, load the file, hit print, then close the application down again. With the lp command, you can send the file directly to the printer from the command line.
This is useful up to a point, but it comes into its own when you have a shell script that needs to print something. Printing existing files, or printing files created by the script itself, is easy with lp and its family of supporting programs.
One point you need to understand though, is you can’t send any old type of file to the printer. For example, you can’t send an ODT file to the printer, because it isn’t in a format that the printer understands. When you open an ODT document in LibreOffice and print it, LibreOffice sends a translated version of the file to the printer, that the printer can understand and act upon. The printing we’re talking about here is best used with text files.
Installing lp
Most distributions include lp and the other printer-related commands straight out of the box. If you do need to install them, these are the commands you’ll need.
On Ubuntu, you need to type:
sudo apt install cups
On Fedora use this command:
sudo dnf install cups
The command for Manjaro is:
sudo pacman -S cups
Finding Out About Your Printer
The lp command has a couple of helpful allied commands. One of these is lpstat, which can find out information about your printer.
Used without any command line options, lpstat displays the print queue.
lpstat
The -p (printers) option lists the configured printers, and their status such as idle or printing.
lpstat -p
The -d (default) option shows the default printer. To get a useful response from this command, you’ll need to have a printer configured as the default device. Just because you have only one printer, that doesn’t make it the default.
lpstat -d
The lpoptions command lists the options that your printer supports. If no printer is specified, the default printer is described.
lpoptions
To find out about a specific printer, you can pass the name on the command line, using the -p (printer) option. As we only have a single printer connected, and it’s the default printer, we’ll get the same result as before.
lpoptions -p Lexmark_B2442dw
Note that the name to use can be found using the lpstat -p output.
To make the output from the lpoptions command more readable, you can pass it through the column command. If we tell it to start a new line whenever it encounters whitespace, we’ll get a rough and ready columnar format that’s much easier to parse.
lpoptions -p Lexmark_B2442dw | column -t -o $'\n'
The lpinfo command can be used with the -v (verbose) option to give you connection details for detected printers. The printers that are able to use each protocol are displayed.
lpinfo -v
Now that we know about our printer, we can send print jobs to it.
Creating Print Jobs With lp
The lp command is the one that actually sends print jobs to the printer.
We can take a quick peek at the print queue with lpq.
lpq
We’re starting with an empty print queue. To send a job, we use the lp command, followed by the name of the file.
lp sample-file-1.txt
If you’re quick enough, or if the file is large enough, you can use lpq to check the job has arrived in the print queue.
lpq
We can add more jobs to the queue, and they’ll be handled in order by the printer as it works its way through its workload.
lp sample-file-2.txt
lpq
If your file has spaces in its name, either wrap the file name in quotes or use tab expansion to complete the name for you. Tab expansion automatically escapes spaces for you.
lp "sample file 3.txt"
lpq
If you need multiple copies of a document, use the -n (copies) option followed by the number of copies you need.
lp -n 15 sample-file-1.txt
lpq
Note that you don’t see 15 entries in the print queue. The job is sent to the printer once, with instructions to print it 15 times.
To conserve paper, and if your printer supports it, you can print double-sided. You can choose to have the pages turned on their long or short edges. We do this by using the -o (options) option, and passing in the name of the option we want to use–in our case, “sides”–and any parameters to that option.
Let’s print something double-sided, with the pages turning on their long edge.
lp -o sides=two-sided-long-edge sample-file-1.txt
To have the pages printed so that the pages were turned on their short edge, we use the two-sided-short-edge parameter.
lp -o sides=two-sided-short-edge sample-file-1.txt
Selecting landscape or portrait is just as easy. We use the -o (options) option once more, and specify either portrait or landscape.
lp -o landcape sample-file-1.txt
There are many other options you can use with the -o (options) option. Check out the man page if you’re interested. You might find some of the options useful.
- job-sheets=name: Prints a cover sheet. Replace name with one of classified, confidential, secret, standard, topsecret, or unclassified.
- media=size: Sets the paper size. Typical sizes are “a4”, “letter”, and “legal”.
- number-up=number: Number can be 2, 4, 6, 9, or 16. This dictates how many input pages are crammed onto one output page.
- print-quality=quality: The quality value can be 3 (draft), 4 (normal), or 5 (best).
Canceling Print Jobs
To cancel a print job you need to know its job number. You can find this in the output of the lpq command.
The command we need is cancel, followed by the job number.
lpq
cancel 16
lpq
You can cancel multiple jobs are once.
lpq
cancel 15 17
lpq
We’ve now emptied the print queue.
Hard Copy, That
Being able to control your printer from the terminal window lets you print from the command line with a degree of control over page layout, and paper handling, but it really comes into its own when you need a way to print from inside a shell script.