Printing Files


The UNIX System includes a collection of programs, called the lp system, for printing files and documents. You can use it to print everything from simple text files to large documents with complex formats. It provides a simple, uniform interface to a wide variety of printers.

The lp system is itself large and complex, but fortunately its complexity is well hidden from users. In fact, three basic commands, lp, lpstat, and cancel, are all you need to know to use this system. (On Linux, these three basic commands have different names; they are lpr, lpq, and lprm, respectively) This section describes how to use these commands to print your files. Administrators will also need to know how to set up and maintain the lp system. The administration of the lp system is discussed in Chapter 13.

Sending Output to the Printer

The basic command for printing a file is lp (line printer) (or on Linux, lpr). This command prints the file research.nov as follows:

 $ lp research.nov request id is lsr1–142 (1 file)

The confirmation message from lp returns a “request ID” that you can use to check on the status of the job or to cancel it if you want. As this example shows, the request ID is made up of two parts: the printer’s name (in this case, lsr1) and a number that identifies your particular request.

You can print several files at once by including all of them in the arguments to lp. For instance,

 $ lp res* request id is lsr1–154 (3 files)

prints all files whose names begin with res.

Specifying a Printer

The lp command does not ask you which printer to use. There may be several printers on the system, but one of them will be the system default. Unless you specify otherwise, this is the printer that lp uses. To find out which printers are available, you can ask your system administrator, or you can use the lpstat command, described in the next section.

Sometimes you want to use a printer that is not the default. To specify a particular printer, use the -d (destination) option, followed by the printer’s name. For example,

 $ lp -d laser2 flightinfo

sends flightinfo to the printer named laser2.

You can change the default printer that lp uses for your jobs by setting the variable LPDEST. The next chapter explains how to set this type of variable, and how to make the change permanent by including it in your shell configuration file.

Print Spooling

When you print a file on the UNIX System, you do not have to wait until the file is printed (or until it is sent to the printer) before continuing with other work, and you do not have to wait until one print job is finished before sending another. lp spools its input to the UNIX print system, which means that it tells the print system what file to print and how to print it, and then leaves the work of getting the file through the printer to the system.

Your job is submitted and spooled, but it is not printed at the precise time you enter the lp command, and lp does not automatically tell you when your job is actually finished. If you want to be notified when it is printed, use the -m (mail) option. For example,

 $ lp -m -d laser2 flightinfo

sends you mail when your file is successfully printed.

If you change the file between the time you issue the lp command and the time it actually goes to the printer, it is the changed file that will be printed. In particular, if you delete the file, or rename it, or move it to another directory the print system will not find it, and it will not be printed. To avoid this, use the -c (copy) option. The command

 $ lp -c -d laser2 flightinfo

copies flightinfo to a temporary file in the print system and uses that copy as the input to the printer. Any changes you make to flightinfo after you issue this command will not appear in the printed output.

Printing Command Output

You use lp to print files. As Chapter 4 explains, the concept of a file in UNIX includes the output of a command (its standard output), so you can also use lp to print the output of a command directly To do this, use the UNIX pipe feature to send the output of the command to lp. For example, the following prints the long form of the listing of your current directory:

 $ ls -1 lp

When you use a pipe to connect the output of a command to lp, the output does not appear on your screen.

Standard output and the pipe mechanism are general features of the UNIX system provided by the shell. They are used in many ways and with many different commands, not just with lp. Chapter 4 covers these in much greater detail.

Using lpstat to Monitor the Print System

Because your print jobs may not print immediately, and because they may be sent to printers located away from your desk, you sometimes need to check on their status. The lpstat command (on LINUX this is the lpq command) provides a way to get this and other useful information, such as which printers are currently available on the system and how many other print jobs are scheduled.

One of the most important uses of lpstat is to see if your print jobs are being taken care of or if there is some problem with the system. The following shows that a job is scheduled for printing but has not yet started printing:

 $ lpstat lsr1–142            jmf           1730   Apr 20 00:29

Suppose you send another file to be printed and then use lpstat to check again:

 $ lp letter.draft $ lpstat lsr1–142            jmf           1730   Apr 20 00:29 on lsr1 sysptr-136          jmf           1930   Apr 20 00:32

This tells you that the first job is now printing on lsr1 and that the second job is scheduled for the default printer, sysptr.

If you need to get a file printed quickly, you may want to check on the status of a printer before you send the job to it. Use the -d (destination) option with the name of the printer. For example,

 $ lpstat -d laser2 laser2 accepting requests since Wed Mar 6 10:23:12 2005 printer laser2 is idle. enabled since Tue Apr 15 10:22:04 2005. available.

shows the status of printer laser2. The -s (system) option shows the status of the system default printer.

To get an overview of the whole print system-what printers are available and how much work each has-use lpstat -t to print a brief summary of the status of the system.

Canceling Print Jobs

Sometimes you need to cancel a print job you have already submitted. You may have used the wrong file, or you may want to make more changes before it is printed. The cancel command (on Linux this command is called lprm) allows you to stop any of your print jobs, even one currently being printed. For example, if lp gave the ID inkjet-133 to one of your print jobs, and you want to stop it, you can use the following command to delete it from the printer system:

 $ cancel inkjet-133 request"inkjet-133" canceled

If you did not save the number of the job when you submitted it, you can get it with lpstat.

Formatting

lp prints exactly what you give it. It does not add anything to the file-no headers, no page numbers, and no formatting. The UNIX System leaves responsibility for all of these sorts of things to formatting programs. The programs pr and fmt can be used for simple formatting, such as adding a header or line numbers to a file before printing it. These commands are described in Chapter 19, in the section “Editing and Formatting Files.”




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net