Help Desk

Today's Best Tech Deals

Picked by Macworld's Editors

Top Deals On Great Products

Picked by Techconnect's Editors

Learn a bit more about the Unix side of OS X, including hints on using Terminal to convert manuals to PostScript, copy information to the Clipboard, convert line breaks in text files, identify what's in a given file, and open the current directory folder in the Finder.

Change End-of-Line Characters

If you're using text files from various sources as inputs for static HTML pages or as scripts for the Apache Web server, you may occasionally find a file that looks right but just doesn't seem to work. For example, instead of a page of text, you see only a blank page in your browser.

It's possible that this problem is caused by an end-of-line character that Unix doesn't understand -- the GUI and Unix sides of OS X use different sets of characters to indicate a new line. If you open the suspect file using the vi editor (change to the file's directory and type vi filename) and see a whole bunch of ^M characters, then you have Mac end-of-line characters in your file. The presence of these characters may cause problems for the built-in Web server, so you'll need to remove them. You could fix the problem by opening the file in Bare Bones Software's BBEdit, selecting Save As, clicking on the Options button, and setting the Line Breaks pop-up menu to Unix.

Terminal, however, provides a quicker and easier solution. For example, to convert all the line breaks from Mac to Unix style in the file foo.txt, you'd type perl -pi -e 's/\r\n?/\n/g' foo.txt.

Perl (Practical Extraction and Report Language) is a type of programming language, and it's doing the dirty work in the conversion. The -pi argument tells Perl to act on every line in the file and to write its changes back to the same file; -e tells Perl to run the following command as a script. The really complex-looking bit is what actually does the work: s is the substitute command, the forward slashes (/) are separators, \r\n? is the string that finds the Mac line endings (a return followed by a new line), and \n is the new Unix line ending (a new line by itself) to be used as the replacement. Finally, g tells Perl to make the substitution globally -- that is, for every match it finds in the file.

When you press enter, the command will execute very quickly, and a follow-up check with vi will show that the Mac line endings are indeed gone.

Read Hidden Perls of Wisdom

If the previous hint has whetted your appetite to learn more about Perl, don't run out and buy a book. Instead, start with OS X itself: an amazingly complete Perl manual is included with the operating system. Open Terminal and type man perl, and you'll get the introductory blurb on the programming language. However, you can learn much more by viewing other sections of the manual, such as Overview and Tutorials. For instance, to read a great introduction to the language, type man perlintro, or type man perlrequick for an explanation of regular expressions (a way of finding patterns of characters in text).

Stop the Senseless Screen Clearing

Have you noticed that certain Unix commands in OS X 10.3 (such as vi, less, and man) finish by clearing the screen? If you're reading a manual page and want to copy and paste a portion of it, having the screen clear when you exit the manual-page viewer is highly annoying.

To solve this problem, make a quick trip to Terminal's preferences and set the Declare Terminal Type ($TERM) As pop-up menu to vt100. The next time you create a Terminal window, you won't be bothered by the unrequested clear-screen action. If you ever want the old behavior back, just set the pop-up menu to xterm-color.

Identify Mystery Files

Have you ever visited a Web site with a script-based downloader, and wound up with a file named something like download.php on your desktop? If you'd like to find out what the file is without dragging and dropping it on all of your expansion utilities, Unix may be able to provide additional detail. Open Terminal, type file download.php, and press enter. If Unix is able to figure anything out about the file, you'll see output like this:

download.php: gzip compressed data, deflated, last modified: Mon Jan 5 12:13:15 2004, os: Unix

In this example, the file has been compressed with the Unix utility gzip and needs to be expanded.

Open the Current Terminal Folder in the Finder

You may already know that you can add a folder path to Terminal by dragging that folder into an open Terminal window. This can be useful when you want to change to a very low-level directory, for instance.

But what if you want to view the folder you're working with in the Finder? Just type open . (notice the space and period) and press enter. The Finder will open a new window displaying the current folder's contents.

Access the Clipboard from Terminal

Although you can use the mouse to highlight, copy, and paste text between Terminal and other OS X applications, you can also directly access the Clipboard via the Unix commands pbcopy and pbpaste. Why use the Unix commands instead of the mouse? Consider ls -al, a command that lists a directory's contents, including ownership and permissions. You may want to e-mail the output to your IT person to troubleshoot a problem you're having. If you were using the mouse, you'd run the command, then highlight its output on screen (scrolling up and down as necessary), press Command-C to copy the selection to the Clipboard, and then press Command-V to paste it into a document or e-mail message.

If you use pbcopy, however, the process is much simpler and faster. Just type ls -al | pbcopy. This command creates the directory listing (ls -al) and then sends the listing to the pbcopy command, courtesy of the pipe (the vertical bar, |). On screen, you won't see any output from your command. But if you switch to your e-mail program and press Command-V, the directory listing will magically appear. Or type pbpaste > listing.txt to paste the Clipboard's contents into a file named listing.txt.

Speak Your Mind

Panther includes a new Unix command, say, for taking advantage of OS X's speech capabilities. (Jaguar users can use osascript -e 'say' to get the same result.) Why might you want to do this? For one thing, it's kind of fun. Type say "hello world" for a basic demonstration of the say command's functionality. But there's much more to it than that; say has some intelligence, too. Type say "I love Mac OS X" and notice how it's pronounced, then try say 723052952 for an example of how the command handles numbers.

If you have remote access to your home Mac via the SSH protocol and there's someone in your house while you're at work, these two commands might be fun to use after remotely connecting to the home machine:

osascript -e 'set volume 10'

say "Help! I'm trapped in this Mac!"

The first line executes an AppleScript that maximizes the volume setting on your machine before speaking what's on the second line.

But there are serious reasons to use say, too. Using its -f and -o options, you can convert a text file into an audio file for use on an iPod or other portable device.

Just type say -f somefile.txt -o spokenfile.aiff, where somefile.txt is the name of the text file and spokenfile.aiff is the name of your output file.

OS X (Panther only) will quickly create an AIFF file, and you won't even have to listen to say read every line in the file as it's created. You might also want to use the -v flag to set which voice to use -- you can specify any of the voices from the Speech preference pane (-v Junior, for instance).

1 2 Page 1
Page 1 of 2
Shop Tech Products at Amazon