Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Chapter 22Name in the book: Printing

Print from the command line

Linux prints with CUPS (Common Unix Printing System). It accepts print jobs, converts them for the printer and keeps a queue. This chapter gives the short history of printing, then shows how to prepare text, send jobs, and watch and cancel them.

  • 6 Lessons
  • 2 Commands

$ run as a normal user# run as root (or with sudo)

22.1

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.

22.2

Prepare text with pr

pr turns plain text into pages with headers and margins before printing.

ls /usr/bin | pr -3 -l 66 -w 65 | head
pr -h "Server report" -o 5 report.txt | lpr
  • -h sets the header text, -o 5 adds a left margin of 5 characters.

22.3

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 optionMeaning
-# nPrint n copies
-pEach page with a header (date, name, page)
-P printerChoose a printer
-rDelete the file after printing
lp optionMeaning
-d printerChoose a printer
-n nNumber of copies
-o landscapeLandscape (wide) page
-o fitplotFit the file to the page
-o scaling=nScale to n percent
-o cpi=n / -o lpi=nCharacters / lines per inch
-P pagesOnly these pages, like -P 1,3-5

Commands in this lesson

22.4

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, a2ps sends the result straight to the printer.
  • Another option with good results: enscript.

Commands in this lesson

22.5

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
CommandWhat it does
lpstat -aWhich printers accept jobs
lpstat -dThe default printer
lpstat -pStatus of each printer
lpstat -tEverything at once
lpqThe queue of the default printer
cancel id / lprm idRemove a job

At work

The CUPS web page http://localhost:631 lets you add printers and watch jobs in a browser.

22.6

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.

All commands in this chapter