6/27/2012

Printing from the command line...

If you're like me (i.e. a neophyte college student), at the end of a semester you have folder for each class, with various sub-folders for hw, lectures, etc. Each folder has a number of files (say, lecture notes), and you'd like to print all of them with a single command (rather than opening each in some program, selecting print, and so forth).

If so, then you're in luck! Via the wonders of the command line (and BASH scripting), the following line of code when run from a terminal (both  OSX and Linux) will do exactly that:

find . -name '*.pdf' -exec lpr {} \;

What the command does:

find . -name '*.pdf'

-searches the directory you are currently in (as well as all sub-directories) for all files ending in .pdf (you can change this to whatever you are trying to print. For instance, if you had some Word files you would change this to .doc)

This is then passed as an input (via the -exec) to the lpr function, which sends a print request for each file found by the 'find' command. The '{}' symbol is what sends the results from find to lpr. Finally, \; ends and executes the line. 

Note: Make sure to only run this script from the directory you want to start searching from, otherwise you'll probably print more than you intended (See some of the stories about golems for further reference).