How to list printer names acceptable for use with lpr?

64

12

The lpr man page says a destination printer can be specified with the -P flag.

-P destination[/instance]
    Prints files to the named printer.

I have 'added' various printers on local Samba shares using the GUI in Ubuntu/Gnome. How can I get a list of these available printers in the format that the -P flag expects (preferably from a bash shell)?

Ian Mackinnon

Posted 2010-08-18T16:23:22.873

Reputation: 3 919

Answers

101

$ lpstat -p -d

From the CUPS manual.

Kevin Panko

Posted 2010-08-18T16:23:22.873

Reputation: 6 339

6Whenever I'm looking for this, I'm also looking for how to pick a default printer: lpoptions -d printername – Joshua Goldberg – 2015-10-26T19:29:52.393

4Thank you! I've just found that lpq works too. – Ian Mackinnon – 2010-08-18T16:56:30.463

13

To get a list you can use:

lpstat -a

or

cat /etc/printcap

To print only the printer names:

lpstat + read + array:

$ while read l; do l=($l); echo "${l[0]}"; done <<< "$(lpstat -a)"

lpstat + awk:

$ lpstat -a | awk '{print $1}'

lpstat + cut:

$ lpstat -a | cut -f1 -d ' '

cat + grep + cut in /etc/printcap:

$ cat /etc/printcap | cut -f1 -d'|' | grep '^#' -v

This is what is shown, one per line:

HP_LaserJet_P1606dn
HP_Deskjet_2540_series
HP_LaserJet_M1212nf
GCP-Save_to_Google_Docs

I feel like the lpstat solutions are more elegant and reliable. Mostly because /etc/printcap was not found on some systems I tested.

About using awk or cut, depends on what you have installed and prefer. The bash read + bash array option should work on any bash shell without the need for externals.

EDIT : I said the marked solution does no work for me on Amazon Linux. But I guess it works if you just want to copy the printer names from the middle of the rest of the output. Works the same as using just lpstat -a.

$ lpstat -p -d
printer HP_Deskjet_2540_series is idle. enabled since Tue 22 Dec 2015 01:12:10 PM BRST
. . .
printer GCP-Save_to_Google_Docs is idle. enabled since Tue 15 Dec 2015 02:13:33 AM BRST
system default destination: HP_LaserJet_P1606dn

Gus Neves

Posted 2010-08-18T16:23:22.873

Reputation: 259

lpstat + cut will work on OS X as well. – tresf – 2016-03-22T16:58:52.727

According to your output sample, lpstat -p -d seems to work… – Skippy le Grand Gourou – 2017-02-08T12:48:13.867

Sorry, but he asks How can I get a list of these available printers in the format that the (lpr) -P flag expects. My example of lpstat -p -d clearly shows that you get more than just the printer name with that. On which case you cannot use that output for a lpr -P $PRINTERNAME call. So, no! lpstat -p -d does not work in the example I gave. – Gus Neves – 2017-04-22T00:33:14.567