home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


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.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.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.