Commands covered in this section: lpr, lpq, lprm, enscript
At the time that UNIX was being developed, line printers were all the rage. This is reflected in the simple printing system used in UNIX. This system consists of a print server (called lpd on many systems), which sends jobs placed in the print queue to the printer, and several client programs that allow you to place jobs in the print queue (lpr), check the status of the print queue (lpq), and remove jobs from the print queue (lprm).
Use the lpr command to send a simple text file to the printer.
$ lpr /etc/hosts
If no printer is specified, lpr will send the print job to the default printer. To specify an alternate printer, use the "-P" option.
Use lpr -P to print to a specific printer.
$ lpr -P myprinter /etc/hosts
Since UNIX is a multi-user system, there's a chance that several users will be sending jobs to the printer simultaneously. To check on the status of your print job, you can use lpq.
Check on the status of the "myprinter" print queue using lpq.
$ lpq -P myprinter Rank Owner Job Files Total Size active root 3 nsswitch.conf 958 bytes 1st dopey 4 (standard input) 1387 bytes 2nd sneezy 5 myfile 5623 bytes 3rd sneezy 6 myfile2 5692 bytes 4rd grumpy 7 hosts 2317 bytes
The display returned by lpq is a table, showing the rank of each pending job, the user who owns the job, the job id, the name of each file, and the size. If you omit the "-P" option, lpq will show the status of the default print queue.
To remove a print job from the queue, use the lprm command. You can remove a particular job by specifying its job id.
Remove a pending job from the "myprinter" print queue using lprm.
$ lprm -P myprinter 6 myprinter-6 dequeued
In this case, the user sneezy is removing job number 6 (myfile2) from the queue.
As with lpr and lpq, omitting the "-P" flag causes lprm to perform the requested operation on the default print queue.
The lpr command usually just copies the specified file into the print queue unmodified, whether it's a simple text file or a PostScript file (PostScript is a page description language developed by Adobe). In some cases, you may want to modify a text file (adding margins, a header, etc.). One command that allows you to do this is enscript (this program was created by Adobe; there are two free clones: genscript and nenscript). These programs are not part of most standard UNIX distributions, but they are often installed by the system administrator.
enscript has many options, but a few of the useful ones include "-G" (add a "gaudy" header, including current date/time, file name, and page number), "-r" (rotate the page orientation to landscape), and "-2" (print two columns instead of one). In order to make these changes to a text file, enscript converts it to PostScript.
Print a text file using enscript with the options specified above.
$ enscript -2Gr -P myprinter /etc/hosts
A sample of the output produced by this command is shown in Figure 6.1.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.