How to print from the Linux command line: double-sided, landscape and more

Getting your Linux system to send print jobs to your local printer might seem like a challenge, especially when you want to print double-sided or in landscape mode, but it’s really a lot easier than you might imagine.

Know your printer

If you’re just starting to use the printer, you might first want to make sure that the lp command is in your search path with a command like this:

$ which lp
/usr/bin/lp

You can then check the status of your printer with an lpstat command like this one:

$ lpstat -p -d
printer HP-Color-LaserJet-CP2025dn is idle.  enabled since Sat 08 Jun 2024 10:20:01 AM EDT
system default destination: HP-Color-LaserJet-CP2025dn

Note that the -p option provides the printer description while the -d specifies the default printer. In this example, there is only one printer. You can use these options separately as shown below.

$ lpstat -p
printer HP-Color-LaserJet-CP2025dn is idle.  enabled since Sat 08 Jun 2024 12:03:54 PM EDT
        Waiting for printer to finish.
$ lpstat -d
system default destination: HP-Color-LaserJet-CP2025dn

If you’re curious about printer options, you can use the lpoptions command like this:

$ lpoptions
copies=1 device-uri=socket://192.168.0.15 finishings=3 job-cancel-after=10800 job-hold-until=no-hold job-priority=50 job-sheets=none,none marker-change-time=1717856401 marker-colors=#000000,#00FFFF,#FF00FF,#FFFF00 marker-levels=5,28,68,27 marker-names='Black Cartridge HP CC530A,Cyan Cartridge HP CC531A,Magenta Cartridge HP CC533A,Yellow Cartridge HP CC532A' marker-types=toner,toner,toner,toner number-up=1 print-color-mode=color printer-commands=AutoConfigure,Clean,PrintSelfTestPage printer-info='HP Color LaserJet CP2025dn' printer-is-accepting-jobs=true printer-is-shared=true printer-is-temporary=false printer-location='HP Color LaserJet CP2025dn' printer-make-and-model='HP Color LaserJet CP2020 Series with Duplexer Postscript (recommended)' printer-state=3 printer-state-change-time=1717856401 printer-state-reasons=toner-low-report printer-type=8425564 printer-uri-supported=ipp://localhost/printers/HP-Color-LaserJet-CP2025dn

To view each option on a line by itself, you can send the output of the lpoptions command to a command like that below which turns the blanks in the output into newlines (‘n’), thus separating the options. Notice all the details provided as you move through them looking at a section at a time with the more command. Even the colors of the toner cartridges are included in this example.

$ lpoptions | tr " " 'n' | more
copies=1
device-uri=socket://192.168.0.15
finishings=3
job-cancel-after=10800
job-hold-until=no-hold
job-priority=50
job-sheets=none,none
marker-change-time=1717856401
marker-colors=#000000,#00FFFF,#FF00FF,#FFFF00
marker-levels=5,28,68,27
marker-names='Black
Cartridge
HP
CC530A,Cyan
Cartridge
HP
CC531A,Magenta
Cartridge
HP
CC533A,Yellow
Cartridge
HP
CC532A'
marker-types=toner,toner,toner,toner
number-up=1
print-color-mode=color
printer-commands=AutoConfigure,Clean,PrintSelfTestPage
printer-info='HP

If you have multiple printers available, you can use the -p option to specify a particular printer.

$ lpoptions -p LaserJet

With the -v option, the lpinfo command will list drivers and related information, including the printer’s IP address.

$ lpinfo -v
file cups-brf:/
network beh
direct hp
network ipps
network lpd
network socket
network http
network ipp
network https
network smb
direct hpfax
network socket://192.168.0.15
network dnssd://HP%20Color%20LaserJet%20CP2025dn%20(F47468)._pdl-datastream._tcp.local/

Now that we’ve taken a look at all the details you can view about your printers, let’s dive into printing.

Using the lp command

To print a document on the default printer, just use the lp command followed by the name of the file that you want to print. If the filename includes blanks (rare on Linux systems), either put the name in quotes or start entering the file name and press the tab key to invoke file completion (as shown in the second example below).

$ lp mystory
$ lp the whole story
request id is HP-Color-LaserJet-CP2025dn-2 (1 file(s))

Use the lpq command to view the files in the printer queue. Note, however, that some files will print so quickly that they’ll be done before you hit the enter key after typing “lpq” and you won’t get a listing. In this case, I put the lpq command on the same line to avoid the print finishing too quickly.

$ lp the whole story ; lpq
request id is HP-Color-LaserJet-CP2025dn-3 (1 file(s))
HP-Color-LaserJet-CP2025dn is ready and printing
Rank    Owner   Job     File(s)                         Total Size
active  shs     3       the whole story                 1024 bytes

Printing multiple copies of a file

With the -n option, the lp command allows you to specify the number of copies of a printout you  want.

$ lp -n 11 agenda

Cancelling a print job

To cancel a print job, you can use the cancel or lprm command. If you don’t act quickly (depending on how many print requests are in the queue), the print job might finish before your cancel command is run:

$ cancel 229
cancel: cancel-job failed: Job #229 is already completed - can't cancel.

Printing in double-sided mode

To print in two-sided mode, you can issue your lp command with a sides option that will allow you to print on both sides of the paper and control which edge the paper will turn on. This setting represents the normal way that you would expect two-sided portrait documents to look.

$ lp -o sides=two-sided-long-edge Notes.pdf

If you want all of your documents to print in two-side mode turning on the short edge, you could change your lp settings by using the lpoptions command to change the setting for sides.

$ lpoptions -o sides=two-sided-short-edge

It’s easy to return the setting to one-sided, though generally wiser to simply specify the option as needed rather than changing the default.

$ lpoptions -o sides=one-sided

Make note of the difference between using an option and changing a default setting. The -o in the first command in this section makes use of an option. The second two commands change settings.

Printing in landscape mode

To print in landscape mode, you would specify the landscape option with the lp command like this followed by the file name.

$ lp -o landscape penguin.jpg

Wrap-up

You have a lot of options when it comes to printing from the Linux command line. Hopefully this post has provided some insights into how to view options and use the lp command’s many options.

Source:: Network World