Printing Under Linux

Note: Machine hal9000 is the print server and should be powered on at all times.

This section discusses how to print files, examine the print queue, remove jobs from the print queue, format files before printing them, and configure your printing environment.

Printing a File Using lpr
For the purpose of security, only the root user and users in the same group as the print daemon are able to write directly to the printer. This is why commands such as lpr, lprm, and lpq have to be used to access the printer.

Because of this, users have to use lpr to print a file. The lpr command takes care of all the initial work needed to print the file, and then it hands control over to another program, lpd, the line printing daemon. The line printing daemon then tells the printer how to print the file.

When lpr is executed, it first copies the specified file to a certain directory (the spool directory) where the file remains until lpd prints it. Once lpd is told that there is a file to print, it will spawn a copy of itself (programmers call it forking). This copy will print our file while the original copy waits for more requests. This allows for multiple jobs to be queued at once.

The syntax of lpr is a quite simple:

$ lpr [ options ] [ filename ... ]


If filename is not specified, lpr expects input to come from standard input (usually the keyboard, or another program's output). This enables the user to redirect a command's output to the print spooler. As such,

$ cat thesis.txt | lpr


or,

$ pr -l60 thesis.txt | lpr


The lpr command accepts several command-line arguments that allow a user to control how it works. Some of the most widely used arguments are: -P printer specifies the printer to use, -h suppresses printing of the burst page, -s creates a symbolic link instead of copying the file to the spool directory (useful for large files), and -#num specifies the number of copies to print. An example interaction with lpr might be something like

$ lpr -#2 -sP A1.23_epson1 thesis.txt


This command will create a symbolic link to the file thesis.txt in the spool directory for the printer named A1.23_epson1, where it would be processed by lpd. It would then print a second copy of thesis.txt.

For a listing of all the options that lpr will recognize.


Viewing the Print Queue with lpq

To view the contents of the print queue, use the lpq command. Issued without arguments, it returns the contents of the default printer's queue.

The returned output of lpq can be useful for many purposes.

$ lpq
lp is ready and printing
Rank Owner Job Files Total Size active 98765432 31 thesis.txt 682048 bytes


Canceling a Print Job Using lprm

Another useful feature of any printing system is the ability to cancel a job that has been previously queued. To do this, use lprm.

$ lprm -



The above command cancels all of the print jobs that are owned by the user who issued the command. A single print job can be canceled by first getting the job number as reported by lpq and then giving that number to lprm. For example,

$ lprm 31


would cancel job 31 (thesis.txt) on the default printer.

Home

Linux Downloads Manuals

Downloading the pcoder

Default Netscape settings

Staff Resource

Student Resource