Printing at the Command Line


While learning to work at the command line, you may have wished on several occasions that you could print the output generated by your commands. Users new to Unix-like operating systems are often surprised to learn that nearly any kind of information can be sent to the printer directly from the command line. In fact, managing printing from the command line is quite easy after a printer has been properly configured. For details on configuring your printer, refer to Chapter 3, "Booting, Logging In, and Configuring."

Printing any kind of information in Linux requires the creation of a print job. When a print job is created, the data to be printed is queued in a list of pending jobs. The print job at the top of the list is the one currently being printed; when it finishes, the system removes it from the queue in a process called dequeueing. The next job moves to the top of the queue, is printed, and so on, until all pending jobs have been printed.

This system enables many users to simultaneously use a Linux system for printing text and data; everyone simply waits his or her turn.

Creating Print Jobs

You create print jobs with the lpr command. To print an existing text file, simply supply it to lpr as an argument:

 [you@workstation20 ~]$ lpr myfile.txt [you@workstation20 ~]$ 

This command creates a print job containing the data from the file myfile.txt; when the job reaches the front of the queue, the file myfile.txt is printed. Data can also be queued using the lpr command in conjunction with pipes, which you learned about in "Using Pipes to Link Commands" in Chapter 19, "Performing Basic Shell Tasks." For example, to print a long listing of the contents of the /etc directory, you pipe the output of the ls command to the lpr command:

 [you@workstation20 ~]$ ls -l /etc | lpr [you@workstation20 ~]$ 

Print jobs created with pipes behave in exactly the same manner as print jobs created from plaintext files.

Listing Print Jobs

In a system in which many print jobs can be queued for printing, users sometimes want to check the list of jobs currently in the queue and their print order. You can use the lpq command to get a list of print jobs. To get a listing of current jobs, enter lpq without arguments on the command line:

 [you@workstation20 ~]$ lpq lp is ready and printing Rank  Owner  Job   File(s)             Total Size active you   3    (stdin)              1024 bytes 1st    you   4    (stdin)            102400 bytes 2nd    you   5    syslog.conf           693 bytes [you@workstation20 ~]$ 

In this instance, three print jobs are in the queue, all started by you. The first two jobs contain output from commands that were piped to lpr. This is indicated by the word stdin (for standard input) appearing in the Files column, meaning that the lpr command is receiving the file to print through a pipe. The last job contains the text in the file syslog.conf.

Dequeueing Print Jobs

If you decide that you no longer want to print a job that's in the print queue, you can dequeue it. The system won't print a job that has been removed from the queue. To dequeue a print job, use the lprm command; the number of the print job from the Job column of lpq's output must be supplied as an argument. For example, if you want to remove the middle print job from the previous example, the correct command is

 [you@workstation20 ~]$ lprm 4 [you@workstation20 ~]$ 

Only the root user or the user who created a job can dequeue it. The root user is able to dequeue all jobs.

Printing to Another Printer

On systems with multiple printers, calling lpr, lpq, and lprm as described here affects only the default printer, usually named lp. However, all three of these commands accept an option, -P, which you can use to specify a different printer.

For example, if you have a second printer named lp2 and want to print myfile.txt to it instead of to lp, add the -P argument followed by lp2:

 [you@workstation20 ~]$ lpr -Plp2 myfile.txt [you@workstation20 ~]$ 

The identical option also can be used to affect the respective behaviors of lpq and lprm.




    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

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