$ run as a normal user# run as root (or with sudo)
Know the short history
Printing went from big shared printers to character printers to graphic printers. The Unix tools still reflect this history.
- In the past computers were shared, and so were printers: jobs had a banner page with the user name.
- Character printers (daisy wheel, dot matrix) printed fixed-width letters: 80 characters per line, about 66 lines per page.
- Graphic printers (laser, inkjet) print pixels. A page description language, mostly PostScript, describes the page. Today PDF is common.
- On Linux, CUPS takes the job and the Ghostscript program converts it into what the printer understands.
Prepare text with pr
pr turns plain text into pages with headers and margins before printing.
Send jobs with lpr and lp
Two families of print commands exist: lpr (Berkeley style) and lp (System V style). CUPS supports both.
ls /usr/bin | pr -3 | lpr
lpr -P printer_name file.txt
lpstat -a
lp -d printer_name -n 2 report.pdf
ls /usr/bin | pr -4 -w 90 -l 88 | lp -o page-left=36 -o cpi=12 -o lpi=8| lpr option | Meaning |
|---|---|
-# n | Print n copies |
-p | Each page with a header (date, name, page) |
-P printer | Choose a printer |
-r | Delete the file after printing |
| lp option | Meaning |
|---|---|
-d printer | Choose a printer |
-n n | Number of copies |
-o landscape | Landscape (wide) page |
-o fitplot | Fit the file to the page |
-o scaling=n | Scale to n percent |
-o cpi=n / -o lpi=n | Characters / lines per inch |
-P pages | Only these pages, like -P 1,3-5 |
Make pretty pages with a2ps
a2ps ("anything to PostScript") formats text nicely: two pages on one sheet, headers, line numbers, syntax colours.
ls /usr/bin | pr -3 -t | a2ps -o ~/Desktop/ls.ps -L 66
a2ps --columns=1 -R script.sh
ps2pdf ~/Desktop/ls.ps ~/Desktop/ls.pdf- Without
-o,a2pssends the result straight to the printer. - Another option with good results:
enscript.
Watch and cancel print jobs
lpstat shows printers and jobs, lpq shows the queue, lprm or cancel removes jobs.
lpstat -a
lpstat -s
system default destination: office
device for office: ipp://192.168.1.50/ipp/print
lpq
office is ready and printing
Rank Owner Job File(s) Total Size
active me 603 (stdin) 1024 bytes
cancel 603
lprm 603| Command | What it does |
|---|---|
lpstat -a | Which printers accept jobs |
lpstat -d | The default printer |
lpstat -p | Status of each printer |
lpstat -t | Everything at once |
lpq | The queue of the default printer |
cancel id / lprm id | Remove a job |
At work
The CUPS web page http://localhost:631 lets you add printers and watch jobs in a browser.
Summary of the chapter
CUPS + Ghostscript do the work; pr and a2ps prepare; lpr/lp send; lpstat/lpq/cancel control.
1Which command lists the printers that accept jobs?
lpstat -a.
2How do you print 2 copies of report.pdf on the printer "office"?
lp -d office -n 2 report.pdf.
3How do you remove print job 603?
cancel 603 or lprm 603.