Remove printer based on queue/path

nckchris
New Contributor

So, we have a situation where our print queue names have changed. Pushing out the new queues and drivers is easy enough, but in the past the printers were often installed manually so the names vary from computer to computer. The queues themselves are the same though.

I found this script:

lpstat -p | cut -d' ' -f2 | grep mcx | xargs -I{} lpadmin -x {}
here courtesy of sam.clark, but my scripting abilities don't go far beyond poorly cobbling things together I find on the internet. Can anyone tell me the best way to modify this, or perhaps a better option to remove a printer based on the path itself rather than the name?

Thanks in advance!

1 ACCEPTED SOLUTION

Look
Valued Contributor III

If you know a string that appears in all printer entries in lpstat this might help. Just watch for false positives if things are similarly named.

#!/bin/bash
for The_Printer in  $(lpstat -a | awk '/COMMON_STRING/ {print $1}'); do
echo removing ${The_Printer}
lpadmin -x ${The_Printer}
done

View solution in original post

2 REPLIES 2

Look
Valued Contributor III

If you know a string that appears in all printer entries in lpstat this might help. Just watch for false positives if things are similarly named.

#!/bin/bash
for The_Printer in  $(lpstat -a | awk '/COMMON_STRING/ {print $1}'); do
echo removing ${The_Printer}
lpadmin -x ${The_Printer}
done

nckchris
New Contributor

Thanks @Look!

That worked perfectly. The only real change I made was to use lpstat -v instead of -a so that I could search by path/queue itself rather than printer name. In case anyone else can make use of it, here is what I ended up using to remove any printers that included "printers.home.school.edu" in the string. For example: smb://printers.home.school.edu/oldprinter

Obviously you could be less specific with awk if need be. Cut just removes the : that follows the printer name so that lpadmin -x will work.

#!/bin/bash

for The_Printer in  $(lpstat -v | awk '/printers.home.school.edu/ {print $3}' | cut -d: -f1); do
echo removing ${The_Printer}
lpadmin -x ${The_Printer}
done

Thanks again for the help. Hopefully someone else can find this useful as well.