Book HomeLearning UnixSearch this book

4.5. Printing Files

Before you print a file on a Unix system, you may want to reformat it to adjust the margins, highlight some words, and so on. Most files can also be printed without reformatting, but the raw printout may not look quite as nice.

Many versions of Unix include two powerful text formatters, nroff and troff. (There are also versions called gnroff and groff.) They are much too complex to describe here. Before we cover printing itself, let's look at a simple formatting program called pr.

4.5.1. pr

The pr program does minor formatting of files on the terminal screen or for a printer. For example, if you have a long list of names in a file, you can format it onscreen into two or more columns.

The syntax is:

pr option(s) filename(s)

pr changes the format of the file only on the screen or on the printed copy; it doesn't modify the original file. Table 4-1 lists some pr options.

Table 4-1. Some pr options

Option

Description

-k

Produces k columns of output.

-d

Double-spaces the output (not on all pr versions).

-h "header"

Takes the next item as a report header.

-t

Eliminates printing of header and top/bottom margins.

Other options allow you to specify the width of columns, set the page length, etc.

Before using pr, here are the contents of a sample file named food:

$ cat food
Sweet Tooth
Bangkok Wok
Mandalay
Afghani Cuisine
Isle of Java
Big Apple Deli
Sushi and Sashimi
Tio Pepe's Peppers
	.
	.
	.

Let's use pr options to make a two-column report with the header "Restaurants":

$ pr -2 -h "Restaurants" food

Oct  6  9:58 2001  Restaurants   Page 1

Sweet Tooth              Isle of Java
Bangkok Wok              Big Apple Deli
Mandalay                 Sushi and Sashimi
Afghani Cuisine          Tio Pepe's Peppers
	.
	.
	.
$

The text is output in two-column pages. The top of each page has the date and time, header (or name of the file, if header is not supplied), and page number. To send this output to the printer instead of the terminal screen, create a pipe to the printer program--usually lp or lpr. The following section describes lp and lpr; Chapter 5 covers pipes.

4.5.2. lp and lpr

The command lp or lpr prints a file (onto paper as opposed to the screen). Some systems have lp; others have lpr. The syntax is:

lp option(s) filename(s)
lpr option(s) filename(s)

Printers on Unix systems are usually shared by a group of users. After you enter the command to print a file, the shell prompt returns to the screen and you can enter another command. However, seeing the prompt doesn't mean that your file has been printed. Your file has been added to the printer queue to be printed in turn.

Your system administrator has probably set up a default printer at your site. To print a file named bills on the default printer, use the lp or lpr command, as in this example:

$ lp bills
request id is laserp-525  (1 file)
$

lp shows an ID that you can use to cancel the print job or check its status. If you need ID numbers for lpr jobs, use the lpq program (see Section 4.5.3.1 later in this chapter). The file bills will be sent to a printer called laserp. The ID number of the request is "laserp-525."

lp and lpr have several options. Table 4-2 lists three of them.

Table 4-2. Some lp and lpr options

Option
lp lpr Description

-dprinter

-Pprinter

Use given printer name if there is more than one printer at your site. The printer names are assigned by the system administrator.

-n#

-#

Print # copies of the file.

-m

-m

Notify sender by email when printing is done.

Windowing applications like StarOffice typically run lp or lpr for you, "behind the scenes." They may have a printer configuration menu entry where you can specify any lp or lpr options you want to use on every print job.

If lp and lpr don't work at your site, ask other users for the printer command. You'll also need the printer locations, so you know where to get your output.

4.5.2.1. Problem checklist

My printout hasn't come out.
See whether the printer is printing now. If it is, other users may have made a request to the same printer ahead of you and your file should be printed in turn. The following section explains how to check the print requests.

If no file is printing, check the printer's paper supply, physical connections, and power switch. The printer may also be hung (stalled). If it is, ask other users or system staff people for advice.

My printout is garbled or doesn't look anything like the file did on my terminal.
The printer may not be configured to print the kind of file you're printing. For instance, a file in PostScript format will look fine when you use a PostScript viewer on your terminal, but look like gibberish when you try to print it. If the printer doesn't understand PostScript, ask your system administrator to install a printer driver that handles PostScript.

You may be trying to print a file directly (with lp or lpr) that should be printed from its own application. For instance, if you have a StarOffice file named report.sdw, you should open that file from a StarOffice window and use the Print command on the StarOffice File menu.

4.5.3. Viewing the Printer Queue

If you want to find out how many files or "requests" for output are ahead of yours in the printer queue, use the program named lpstat (for lp) or lpq (for lpr). The cancel program lets you terminate a printing request made by lp; lprm cancels jobs from lpr.

If you have a graphical application such as StarOffice that does its printing with lp or lpr, you should be able to use these commands to check and cancel those print jobs.

4.5.3.1. lpstat and lpq

The lpstat program shows what's in the printer queue: request IDs, owners, file sizes, when the jobs were sent for printing, and the status of the requests. Use lpstat -o if you want to see all output requests rather than just your own. Requests are shown in the order they'll be printed:

$ lpstat -o
laserp-573  john  128865  Oct 6  11:27  on laserp
laserp-574  grace  82744  Oct 6  11:28
laserp-575  john   23347  Oct 6  11:35
$

The first entry shows that the request "laserp-573" is currently printing on laserp. The exact format and amount of information given about the printer queue may differ from system to system. If the printer queue is empty, lpstat says "No entries" or simply gives you back the shell prompt.

lpq gives slightly different information than lpstat -o:

$ lpq
laserp is ready and printing
Rank   Owner      Job  Files                  Total Size
active john       573  report.ps              128865 bytes
1st    grace      574  ch03.ps ch04.ps        82744 bytes
2nd    john       575  standard input         23347 bytes
$

The first line displays the printer status. If the printer is disabled or out of paper, you may see different messages on this first line. The "active" job, the one being printed, is listed first. The "Job" number is like the lpstat request ID. To specify another printer, add the -P option (Table 4-2).

4.5.3.2. cancel and lprm

cancel terminates a printing request from the lp program. lprm terminates lpr requests. You can specify either the ID of the request (displayed by lp or lpq) or the name of the printer.

If you don't have the request ID, get it from lpstat or lpq. Then use cancel or lprm. Specifying the request ID cancels the request, even if it is currently printing:

$ cancel laserp-575
request "laserp-575" cancelled

To cancel whatever request is currently printing, regardless of its ID, simply enter cancel and the printer name:

$ cancel laserp
request "laserp-573" cancelled

The lprm program will cancel the active job if it belongs to you. Otherwise, you can give job numbers as arguments, or use a dash (-) to remove all of your jobs:

$ lprm 575
dfA575diamond dequeued
cfA575diamond dequeued

lprm tells you the actual filenames removed from the printer queue (which you probably don't need).

4.5.3.3. Exercise: manipulating files

In this exercise, you'll create, rename, and delete files. First you'll need to find out if your site has one or more printers and the appropriate command to use for printing.

Go to home directory.

Enter cd

Copy distant file to working directory.

Enter cp /etc/passwd myfile

Create new directory.

Enter mkdir temp

List working directory.

Enter ls -F

Move file to new directory.

Enter mv myfile temp

Change working directory.

Enter cd temp

Copy file to working directory.

Enter cp myfile myfile.two

Print the file.

Enter your printer command and the filename (if the file is long, you may want to edit it first--with Pico, for instance)

List filenames with wildcard.

Enter ls -l myfile*

Remove files.

Enter rm myfile*

Go up to parent directory.

Enter cd ..

Remove directory.

Enter rmdir temp

Verify that directory was removed.

Enter ls -F



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.