On this page…
6: Printing and Managing Print Jobs
Over the years, Linux has had several printing systems, including the venerable Line Printer Daemon (LPD) and LPR Next Generation (LPRng) that are still found in vestigial form on modern Linux distributions. In the past few years, however, most distributions have settled on CUPS (which formerly stood for the Common UNIX Printing System, but has since been shortened to just plain ol’ CUPS) as their backend of choice. CUPS is well supported, easy to use, modern, and a perfect drop-in replacement for LPD and LPRng. The same commands used with LPD and LPRng still work, but now they call functions in CUPS.
This chapter focuses on CUPS because it is the printing system with which most Linux users work. This chapter does not cover how to set up and configure a printer. Most distributions now provide easy-to-use GUI configuration tools to do just that, so you’re going to focus on actually querying and using the printer via the command line.
Linux Journal’s “Overview of Linux Printing Systems,” available at www.linuxjournal.com/article/6729, provides an excellent look at the various options Linux users have today, with special focus on the current favorite, CUPS. For more on CUPS, see Linux Journal’s “The CUPS Printing System” at www.linuxjournal.com/article/8618, a very good look at this essential technology. The best place to go for information about CUPS is, unsurprisingly, the CUPS Software Users Manual, which you can find at www.cups.org/doc-1.1/sum.html. It’s long and sometimes obtuse, but full of valuable advice and help, and it’s an essential resource.
List All Available Printers
Before you can begin working with your printers, you need to know what makes up “your printers.” To find the printers configured on your system, use the
lpstat command (short for line printer status) along with the
$ lpstat -p printer bro is idle. enabled since Jan 01 00:00 printer bro_websanity is idle. enabled since Jan 01 00:00 printer wu_eads14 is idle. enabled since Jan 01 00:00
As you can see, this system has three printers—
wu_eads14 (bro is short for Brother, the manufacturer of those printers)—and none of them are printing anything at the moment.
Determine Your Default Printer
You know all of your printers, thanks to running
lpstat -p, but which one is the default? As you’re going to see soon, you can send a print job to a specific printer, or you can quickly send it to your default printer. To find out which printer is the default, use the
lpstat command with the
-d (for default) option.
$ lpstat -d system default destination: bro
Of course, if you only have one printer connected to your computer, you probably don’t need to run this command. But for laptop users who move around to different locations and who print to different printers, this command is essential.
Find Out How Your Printers Are Connected
Laptop users find this next command particularly helpful, as it tells them how they access the printers available to them. When you first set up a printer, you must specify how you connect to it. You have several choices:
- Local (parallel, serial, or USB)
- Remote LPD queue
- SMB shared printer (Windows)
- Network printer (TCP)
- Remote CUPS server (IPP/HTTP)
- Network printer with IPP (IPP/HTTP)
To find out what printers are configured for your computer and how you connect to those printers, use
lpstat with the
$ lpstat -s system default destination: bro device for bro: socket://192.168.0.160:9100 device for bro_websanity: socket://192.168.1.10:9100 device for wu_eads14: socket://220.127.116.11:9100
In this case, every printer is a network printer, so it uses
socket://, followed by the printer’s IP address and its port (
9100 is standard for most networked printers, although you might see port
35 used as well). That’s pretty easy, but it can quickly get much more complicated.
Although CUPS is user-friendly in many areas, it’s famously obtuse when it comes to the Uniform Resource Indicators (URIs) used to indicate the locations of printers vis-á-vis your Linux box. Table 6.1 lists each connection method and the type of URI you might see, which should help you understand the list of printers and URIs you see when you run
Assume that the printer in the following examples is named
bro and located on the network at
192.168.0.160. That isn’t relevant in every situation, of course. If the printer is connected via a parallel cable, its IP address doesn’t matter.
|Remote LPD queue||
|SMB shared printer (Windows)||
|Network printer (TCP)||
|Remote CUPS server (IPP/HTTP)||
|Network printer with IPP (IPP/HTTP)||
Thanks to the rise of network printing in the past several years, it’s getting simpler to connect to printers via
http. Even so, you’re still going to run into legacy printers that require the older, more complicated connection methods, so it’s good to familiarize yourself with them.
A bonus is that
lpstat -s in essence duplicates the functionality of
lpstat -p -d, as it lists all printers known by your system, as well as the default printer. If you want to know all that information quickly, this is a good command to use.
Get All the Information About Your Printers at Once
Now for the mighty command. The nice thing about using
lpstat -d, or
lpstat -s is that you only get the precise bit of information you want. If you want everything all at once, however, use
lpstat with the
-t option, which dumps everything
lpstat knows about your printers onto your shell.
$ lpstat -t scheduler is running system default destination: bro device for bro: socket://192.168.0.160:9100 device for bro_websanity: socket://192.168.1.10:9100 device for wu_eads14: socket://18.104.22.168:9100 bro accepting requests since Jan 01 00:00 bro_websanity accepting requests since Jan 01 00:00 wu_eads14 accepting requests since Jan 01 00:00 printer bro is idle. enabled since Jan 01 00:00 printer bro_websanity is idle. enabled since Jan 01 00:00 printer wu_eads14 is idle. enabled since Jan 01 00:00
You get it all: your default printer, a list of all printers known to your system, the connection methods and locations of those printers, and the status of all printers. The more printers you have configured on your computer, the longer this listing. For some of you, it will be overwhelming, so you might find that
lpstat with one of the other options you’ve examined is a better choice.
Print Files to the Default Printer
Now that you know what printers are on your system, it’s time to actually use them to print something. Printing to your default printer (determined with
lpstat -d) is easy.
$ lpr Lovecraft_-_Call_of_Cthulhu.txt
That’s it: Just
lpr and the name of the text file. Pretty simple.
You probably expect that you can print ASCII text files on the command line using CUPS, but it might surprise you to learn that you can also print PDFs or PostScript files. But that’s it; don’t try to print Word, OpenOffice.org, or any other kind of non-text-based or non-PostScript-based documents, or your printer will spew out pages of garbage!
Print Files to Any Printer
Printing to the default printer, as shown in the previous section, is pretty easy. If you have more than one printer, however, and you want to print to one that is not the default, simply use the
-P option, followed by the name of the printer.
$ lpr -P bro_websanity Lovecraft_-_Call_of_Cthulhu.txt
If you don’t know the names of your printers, use
lpstat -p, as discussed previously in the “List All Available Printers” section.
You’ll notice that the filename has underscores instead of spaces, which makes it much easier to deal with on the command line. If the filename has spaces in it, you have to use one of the following methods for referencing it:
$ lpr -P bro_websanity "Lovecraft - Call of Cthulhu.txt"
$ lpr -P bro_websanity Lovecraft\ -\ Call\ of\ Cthulhu.txt
If the filename does have spaces, it’s probably easiest to use tab completion so you don’t have to type out the name yourself. For instance, you’d enter
lpr -P bro_websanity Love and then press the Tab key, allowing bash to finish the filename. For more on tab completion, see <www.slackbook.org/html/shell-bash.html#SHELL-BASH-TAB>.
Print More Than One Copy of a File
If you want to print more than one copy of document, use the
-# (the pound sign) option, followed by the number of copies you want:
$ lpr -# 2 -P printer Lovecraft_-_Call_of_Cthulhu.txt
The number can range from 1 to 100. Want more than 100? Repeat the command. Or write a script. Or hire a professional printer!
List Print Jobs
If you have several jobs queued up to print, you might want to see that list. Perhaps you want to cancel one or more of the jobs (more on how to do that ahead, in the next few sections), you want to find out why a job is taking so long to print, or maybe you just want to know how many print jobs are in line. The
lpq command (as in lp queue) lists all jobs currently printing on the default printer.
$ lpq bro is ready and printing Rank Owner Job File(s) Total Size active scott 489 Lovecraft_-_Call_of_C 108544 bytes
If you want to find out the status of print queues for all your printers, not just your default, simply append
-a (as in all) after
$ lpq -a Rank Owner Job File(s) Total Size active scott 489 Lovecraft_-_Call_of_C 108544 bytes 1st scott 490 ERB_-_A Princess_of_M 524288 bytes
Keep in mind two things. First, the listing provided by
lpq -a is cut off, so while the filenames are actually
ERB_-_A Princess_of_Mars.txt, you don’t see the entire name because
lpq only shows a certain number of characters.
Second, and this is very important,
lpq is not showing all the print jobs the printer knows about, only those that your machine knows about. From the printer’s perspective, the print queue might actually look like this:
- ERB_-_A Princess_of_Mars.txt
To find out the actual queue on the printer, you have to use whatever management utilities come with that device, which is far beyond the scope of this book.
List Print Jobs by Printer
lpq command shows you the files that are queued to print, but it doesn’t tell you to which printer the files are being sent. To learn that, you need to use the
lpstat command, covered earlier in “List All Available Printers.” This time, however, simply use
lpstat without any options at all:
$ lpstat bro-489 rsgranne 108544 Tue 10 Dec 2005 03:00:09 PM CST bro_websanity-490 rsgranne 524288 Tue 10 Dec 2005 03:05:05 PM CST
The result is a list of print jobs, with the name of each printer handling the job at the beginning of each line. Did you accidentally send a job to a printer to which you’re not currently connected? Find out with
lpstat and then delete the job, which you’ll learn in the next section.
Cancel the Current Print Job Sent to the Default Printer
Do you want to cancel the current print job being sent to your default printer? Just use
lprm (as in lp remove):
Be sure to issue this command with haste. Many printers now are so fast and have so much memory that the job might have already left your machine and be running on the printer. In that case, find the Cancel button on your printer and press it quickly!
Cancel a Print Job Sent to Any Printer
lprm job ID
In the previous section, you learned how to cancel the current print job going to the default printer. But what if the job is queued and won’t start for another few minutes? Or what if the job is heading for a printer that’s not the default? In those cases, you want to still use
lprm, but you want to tell the command which job to cancel by referencing the job’s ID number.
Look back at the examples in “List Print Jobs.” The third column is labeled “Job” and has a number in it. Now look back at “List Print Jobs by Printer.” After the name of each printer is a hyphen and then a number—the same number seen in “List Print Jobs.” That number is the job ID number. Specify that number to
lprm, and that exact job will be removed.
$ lpstat bro-489 rsgranne 108544 Tue 10 Dec 2005 03:00:09 PM CST bro_websanity-490 rsgranne 524288 Tue 10 Dec 2005 03:05:05 PM CST $ lprm 490 $ lpstat bro-489 rsgranne 108544 Tue 10 Dec 2005 03:00:09 PM CST
You can really save yourself from a needless waste of paper, toner, and ink using this particular command. Next time you realize that you just sent a 500-page document full of large pictures to your printer and stop it using
lprm and a job ID number, send an email my way and thank me.
Cancel All Print Jobs
What if you have several print jobs you want to cancel? You could delete them all by specifying all of their job ID numbers, like this:
$ lprm 489 490 491 492 493
That’s too much typing, however. If you want to get rid of all print jobs you’ve queued on every printer, just follow
lprm with a hyphen:
$ lprm -
It’s quick, it’s painless, and it’s lazy in the best Linux tradition. Nothing to be ashamed of there!
Printing is a central activity people perform on their computers, so it’s important to learn how to print effectively. Part of printing is knowing how to query printers and jobs as well as send those jobs to printers in the first place. Of course, not all jobs need to be printed, so it’s important to know how to cancel unnecessary jobs as well. This chapter covers the major tasks you’ll need to work with your printers from within Linux, but let me leave you with a final bit of advice: Automatic duplex printers that work with Linux have now fallen in price drastically, and they’ll help you save a small fortune in paper. Check one out when you’re next in the market for a printer. You’ll be glad you did.