Advertisement
Advertisement
Advertisement

Terminal Tips: Print PDFs from the Command Line

lpr


Yesterday, Phil Windley noted a solution he discovered for printing multiple PDFs at once from the command line, using lpr. He didn't want to waste the time to open all the PDFs in Acrobat or Preview and then select print for each of them separately. Instead, he launched the Terminal (located in /Applications/Utilities/), and typed:

for f in *.pdf
> do lpr -PCSOffice $f
> done

The first line defines f as being a set of all the files ending with .pdf within the current directory. The do command is a shell command, basically telling your Mac, "Hey, I want you to do this!" lpr is the print command, the -P followed by the name of your printer says, "Hey, use this printer!" If you don't set a default one here, then lpr will simply use your default printer. $f tells lpr that you want to print everything within the f set that you defined in the first line. Then done simply says, "Okay, I'm finished giving you commands related to f."

Read more after the jump...

You can use lpr to print a folder full of PDFs or text documents. If you want to find out more about lpr, you can either type man lpr in the Terminal and hit Return, or see the PDF file Windley references. In the screenshot accompanying this post, I listed all the contents of my home folder using ls and then ran the same set of commands to print all of the files ending in .txt within that folder (only two) to my default printer.

When I had Jay proof this post, he noted that the whole thing could be done in one line of code:

for f in *.pdf; do lpr -Pprinter $f; done

Replace printer with the name of the printer you want to use or simply remove -Pprinter to print to the default printer.

Advertisement
Advertisement