Полезная информация

Learning the Unix Operating System

Learning the Unix Operating SystemSearch this book
Previous: 4.4 Managing Your FilesChapter 4
File Management
Next: 5. Redirecting I/O

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 command 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.2 lists some pr options.

Table 4.2: Some pr Options
-kProduces k columns of output.

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

-h "header"Takes the next item as a report header.
-tEliminates printing of header and top/bottom margins.

Other options allow you to specify the width of the columns, set the page length, and so on.

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

% cat food
Sweet Tooth
Bangkok Wok
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

Nov  7  9:58 1997  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, you 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

If you have a long file, it may be best to print it so you can see it all on paper. The command lp or lpr prints a file (onto paper as opposed to the screen display). Your system will probably have one or the other - but not both. 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 command (see "lpstat and lpq" in the following section). 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.3 lists three of them.

Table 4.3: Some lp and lpr Options

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-mNotify sender by email when the printing is done.

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.3 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 section below explains how to check the print requests.

If no file is printing, check the printer's physical connections and power switch. The printer may also be hung. If it is, ask your system administrator what to do.

4.5.4 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 command lpstat (for lp) or lpq (for lpr). The cancel command lets you terminate a printing request made by lp; lprm cancels jobs from lpr. lpstat and lpq

The lpstat command 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  Nov 7  11:27  on laserp
laserp-574  grace  82744  Nov 7  11:28
laserp-575  john   23347  Nov 7  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 will say "No entries" or simply give 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, and the P option (Table 4.3). cancel and lprm

cancel terminates a printing request from the lp command. 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 command 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). Exercise: Manipulating files

In this exercise, you'll create, rename and delete files. Find out if your site has one or more printers as well as 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

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

Previous: 4.4 Managing Your FilesLearning the Unix Operating SystemNext: 5. Redirecting I/O
4.4 Managing Your FilesBook Index5. Redirecting I/O

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System