Printing Files

Printing Files

Now that you are creating files from the command line, you will undoubtedly want to print them. Of course, you can open the files in a GUI text editor or word processor and print using the standard Mac print dialog, or simply select text in your Terminal window and choose Print from the Shell menu. But once you start using the command line, you will often want to stay there and not switch to a GUI application unless you absolutely have to. That means you'll want to print from the command line.

Also, you may want to create a script that sends information to a printer, so you will need a command line you can put in the script.

Here's the very short version (see the rest of this section for more gory details):

lp filename

You can print only certain types of files from the command line. Generally , you can print files in standard file formats such as plain text, PostScript, GIF, PDF, JPEG, and TIFF, but you cannot print proprietary file formats from the command linefor example, files in the native format of the applications that created them. That means no AppleWorks or Microsoft PowerPoint files. Those need to be printed from within the applications that created them; the applications actually convert the file to another format for printing.

Unix Pioneer: Richard M. Stallman

Richard M. Stallman (known in the Unix community by his lowercase initials , rms) is a programmer with a major vision of programming as an art form that should share its fruits with all of society.

The reputation of rms in the Unix world is that of a passionate , dedicated, visionary , and dogmatic advocate for free software ( free as in freedom , not as in free beer ).

His contributions to the Unix world have been crucial in making Unix as open and flexible as it is today. He is the principal author of some vital pieces of Unix software and is the founder of the Free Software movement. This is a worldwide effort to create and maintain software with which users have the freedom to examine and change its inner workings and to share their changes with anyone they wish.

One key piece of software that rms is responsible for is the GNU C compiler (gcc), the most widely available compiler in the world.

Besides creating a number of important pieces of software, rms also created the basic license agreement under which most open-source Unix software is distributed. The GNU GPL (General Public License) gives permission to use a piece of software, to examine and alter its source code, and to redistribute the altered version of the software, but only if the altered source code is also distributed under the same GNU GPL license. The Linux operating system is the most well-known collection of GNU GPL software.

Richard M. Stallman's personal Web site is; to learn more about the Free Software Foundation, visit and

How printing works on a Macintosh

Starting in Mac OS X 10.2, all printing is handled using the Common Unix Printing System, or CUPS (see the sidebar). Table 6.5 lists some command-line utilities for use with CUPS.

Table 6.5. CUPS Printing Utilities



Get status

lpstat information

Export printer

cupsaddsmb for Windows

Manage printers

lpadmin or http://localhost:631/printers/

It is important to understand that printers generally can only print documents that are sent to them already converted into a format the printer can understand. For example, printers are not able to understand a Microsoft Excel spreadsheet or a JPEG image.

When you print from within a conventional Macintosh application such as Adobe Photoshop, AppleWorks, or Excel, the application converts the document from the application's native format to a format that the Macintosh printing system can handle (typically PDF for Mac OS X applications and PICT format for Classic applications). The Mac printing system then does another conversion and hands the result to the actual printer.

Some printers expect documents to be sent to them as PostScript files, and other printers expect documents to be formatted using a proprietary page-description language like Hewlett-Packard's PCL (Printer Control Language), and still others desire what is called Raster input (each dot's position is explicitly specified). This is why each printer needs software specifically designed for that printer. In Mac OS X Classic mode (and Mac OS 9 and earlier), the printer-specific software shows up in the Chooser. In Mac OS X, Apple has preinstalled the software that converts files to the format used by each supported printer (called a filter ). You can also install new filters, which is what often happens when you install the software for a new printer.

CUPSthe Common Unix Printing System

CUPS is an open-source printing system for Unix. It uses its own Web server (listening on port 631) to receive printing requests . In Mac OS X, this is limited to your own machine unless you allow outside access.

The CUPS software attempts to determine what kind of file you are printing and passes it to the appropriate CUPS filter for conversion into the format used by your printer. You can add filters as they become available.

CUPS comes with extensive documentation and a very nice Web interface for managing print jobs. The CUPS homepage is You can see a basic overview of the CUPS system at http://localhost:631/overview.html. The CUPS documentation is at http://localhost:631/documentation.html. The user 's manual is http://localhost:631/documentation.html. And the actual management system is at http://localhost:631/, but please be careful not to simply start adding and deleting printers without any knowledge of how that can mess up your Mac OS X printing!

Two Ways to Print: lp vs. lpr

The lp ( line printer ) and lpr ( line printer request ) commands are the two most common Unix commands for printing. In Mac OS X, both commands have been configured to use CUPS.

The lp command was originally developed as part of BSD Unix, and the lpr command comes from the AT&T System V version of Unix.

In Mac OS X you will see no practical difference between the commands.

The two commands do have different options, as comparing the man pages will show.

Printing PostScript

If you have Mac OS X 10.3 or later, you do not need a PostScript printer to print PostScriptCUPS will convert the PostScript file for you.

The version of CUPS installed with Mac OS X before version 10.3 will not convert PostScript files for printing on a non-PostScript printer. However, you can install additional software that supports this, enabling you to print PostScript files to non-PostScript printers. See Gimp-Print (http://gimp-print. sourceforge .net/MacOSX.php3).

Printing from the command line

You print documents from the Mac OS X command line using the lp or lpr command (see the sidebar "Two Ways to Print: lp vs. lpr "). Wherever we refer to the lp command in a task, you can substitute the lpr command if you like. In either case, the file(s) you print will be passed through CUPS, and CUPS will use a filter to convert the file(s) into a format that your printer can understand.

Both lp and lpr take filenames as arguments, passing the files to CUPS for processing. You can also pipe the output of any command line into lp or lpr . As long as the command output is a format that CUPS supports, it prints properly.

The CUPS software recognizes most standard file types, such as plain text and GIF, JPEG, PNG, and TIFF images, as well as PDF and PostScript files.

CUPS converts the file to the format your printer uses, assuming that your printer is one of the many supported by CUPS.

Note that HTML files print as plain text, not as you see them in a Web browser. That is because HTML files are actually plain-text files containing the source code that tells a Web browser what to display.

Support for additional printers is added either through software updates from Apple (see the Software Update tool in System Preferences) or by downloading new printer drivers and installing them (see the sidebar "Support for More Printers and File Formats").

To print a single file from the command line:

  • lp filename

    Here the lp command sends the file named filename to your default printer. For example, you can print the 44-page-long Software Users Manual for CUPS with

    lp /usr/share/doc/cups/sum.pdf

    Here's a file with only one page:

     lp "/Library/Desktop Pictures/Black &  White/Mojave.jpg 

To print multiple files:

  • lp file1 . . .

    Just supply all the filenames as arguments on the command line. For example,

    lp hello.pdf flower.jpg smiles.tiff

    prints all three files.

To print the output from a command:

  • command line lp

    For example,

    find /Users/ vanilla - name "*.png" lp

    prints a list of all the .png files in vanilla's home directory. The output of the command line is sent to lp .

    As long as the output is a format recognized by CUPS, it prints properly, so commands that produce output in the form of image files, PDF, and PostScript can all be sent directly to the lp command. For example,

    man -t ssh lp

    pipes the PostScript output of the man command (because of the -t option) to the lp command, which pushes it through CUPS, where it eventually gets to the printer.

To see information about printers:

  • lpstat -p

    The lpstat command is used to obtain information about CUPS. The -p option displays the list of available printers ( Figure 6.21 ).

    Figure 6.21. Using lpstat -p to see a list of available printers.
     localhost:~ vanilla$  lpstat -p  printer HP_Color_LaserJet_4500 is idle.  enabled since Jan 01 00:00 printer SC_860hUSBi is idle.             enabled since Jan 01 00:00 localhost:~ vanilla$ 

    There are several more useful options to lpstat ; for example, the -d option shows the current default printer ( Figure 6.22 ), the -R option shows all jobs waiting to print, and the -t option shows all available information. See man lpstat for a list of all options.

    Figure 6.22. Using lpstat -d to see the system default printer.
     localhost:~ vanilla$  lpstat -d  system default destination: SC_860hUSBi localhost:~ vanilla$ 

Support for More Printers and File Formats

Apple adds support for additional printers every so often, and these are available to you via the online Software Update system (see Software Update in System Preferences).

Support for additional printers and file formats (as when printing PostScript files to non-PostScript printers) is also available through the hard work of many open-source programmers. An excellent resource for upgrading your CUPS system is the Gimp-Print project ( Look for the Mac OS X link.

Gimp-Print is a source of high-quality open-source print drivers, and you will almost certainly see improved print quality using its software.

Gimp-Print, in combination with the ESP-Ghostscript software (both available at the Gimp-Print Web site), enables you to print PostScript files to non-PostScript printers such as the USB inkjet printers that are common for home and small-office use.

To set the default printer:

  • sudo lpadmin -d destination

    The sudo command attempts to run the rest of the command line as rootthat is, with the maximum privileges possible on a Unix system. The sudo command is covered in detail in Chapter 11, "Introduction to System Administration," but for now you can just enter the command line as shown above. Trust me.

    If you haven't used the sudo command within the previous few minutes, you will be asked for your password; enter it.

    You are using the lpadmin command to set the default printer.

    The destination is a printer name from the list supplied by the lpstat -p command (described above). For example,

     sudo lpadmin -d sudo lpadmin -d  SC_860hUSBi 

To print to a non-default printer:

  • lp -d destination filename

    Use the -d option to specify a printer name (as reported by lpstat -p )for example,

     lp -d HP_Color_LaserJet_4500 

    It is critical that the printer name be entered exactly as reported by lpstat . So if the printer name has spaces in it, you must either enclose it in quotes or escape the spaces with \ .

    Note: If you are using the lpr command, use the -P option instead of -d .

Printing to AppleTalk printers

This is actually pretty easy. You can print files on AppleTalk printers using the atprint command. First you have to select the printer from the command line, but only onceafter that your selection is remembered .

As a disclaimer, since we no longer have access to AppleTalk printers, we were unable to test this section on Tiger for this edition of the book. However, the information from the last edition is still helpful for those of you using AppleTalk printers.

To select an AppleTalk printer with at_cho_prn:

sudo at_cho_prn

at_cho_prn means "AppleTalk choose printer." The sudo command attempts to run the rest of the command line as rootthat is, with the maximum privileges possible on a Unix system. Remember that the sudo command is covered in detail in Chapter 11, but for now you can just enter the command line as shown above.

If you haven't used the sudo command within the previous few minutes, you will be asked for your password; enter it.

When the at_cho_prn command runs, it shows you a list of the AppleTalk zones your machine can see ( Figure 6.23 ).

Figure 6.23. The at_cho_prn command shows you a list of available AppleTalk zones.

Enter the zone number to search, and then press .

Once you enter a zone number, at_cho_prn searches the zone for printers ( Figure 6.24 ) and displays a numbered list of all the printers found.

Figure 6.24. Once you select a zone, at_cho_prn searches for printers in that zone.

Enter the printer number from the list.

You select a printer by typing its number and pressing ( Figure 6.25 ).

Figure 6.25. You select a printer in at_cho_prn .

You're done.

The selected printer will now be used for command-line printing with the atprint command.

To print to an AppleTalk printer:

  • atprint < filename

    For example,

    atprint <

    The atprint command sends its input to an AppleTalk printer (the last printer selected using at_cho_prn , described above).

    You do not supply the filename to print as an argument to atprint . Rather, you must supply the file contents as input to the command, using the < input redirection operator (see Chapter 2) or piping the output of a command to atprint . For example,

    last -25 vanilla atprint

    sends the output of the last command to the default AppleTalk printer.


  • You can override the default printer and specify the printer you want to use as an argument to atprint for example,

     atprint "HP Color LaserJet  4500: LaserWriter@Zone1  < myfile.pdf 

    See the man page for a more detailed description of available options.

Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: