May 28, 2011, 10:05 a.m.
posted by homene
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
[[email protected] ~]$ lpr myfile.txt [[email protected] ~]$
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:
[[email protected] ~]$ ls -l /etc | lpr [[email protected] ~]$
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:
[[email protected] ~]$ 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 [[email protected] ~]$
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
[[email protected] ~]$ lprm 4 [[email protected] ~]$