24

It is very difficult for me to perform printing, because the printer dialogue is full of hundreds of automatically added printers, so I can't find the one I want to use, see screenshot below.

Image of printer dialogue showing a very long scroll bar

The printers stem from my work network, where apparently lots of people (including students, I guess) are "sharing" their home printers, which my laptop then picks up automatically. (coincidentally I sometimes disable the avahi-daemon at work, simply because it is using a large amount of CPU).

When I go to http://localhost:631/printers/, it says there are 131 printers, and they are all of Make and Model 'Local Raw Printer'. With two exceptions: 1 is a network printer at work that I manually configured. Another is a network printer at my parents', which was also automatically added and which I am on the same network as right now. But the rest are just garbage that I would really like to avoid. Tips on how to do that would be appreciated.

I will not be going back to my work place for a bit of time though, so for now, I would just like these printers (that would be the 129/130 automatically added printers) to be removed. Is there a way to do that? I guess I could do it by clicking through in the cups web interface, but for 129 printers, that is a bit much. So I am looking for a single command or tips on how to achieve it with a script of sorts.

George Udosen
  • 37,534
jonaslb
  • 342

1 Answers1

18

Using this command: lpstat -a we can see the installed printers and identify the name of the desired printer to keep, we can use the grep command also to filter the results like so: lpstat -a | grep <probable_name_of_printer>.

Then this little script can help:

  1. Run this command to check that the desired printer is not listed:

    lpstat -a | cut -d" " -f1 | sed -E '/<NAME_OF_PRINTER>/d' | grep <NAME_OF_PRINTER>
    
    • This should return nothing as it does the following:

      • lpstat -a: list installed printers
      • cut -d" " -f1: return only the names of the printers
      • sed -E '/<NAME_OF_PRINTER>/d': remove the name of the printer to keep from the output of the previous commands
      • grep <NAME_OF_PRINTER>: make sure the desired printer is not on the list
  2. If the above checks out; then run this command to remove every other printer that you don't need:

    sudo bash -c 'for i in $(lpstat -a | cut -d" " -f1 | sed -E '/<NAME_OF_PRINTER>/d'); do lpadmin -x "$i"; done'
    
Jos
  • 30,529
  • 8
  • 89
  • 96
George Udosen
  • 37,534