O'Reilly logo

Learning Unix for Mac OS X Tiger by Dave Taylor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Looking Inside Files

By now, you're probably tired of looking at files from the outside. It's like visiting a bookstore and never getting to open the book and read what's inside. Fortunately, it doesn't have to be this way, so let's look at three different programs for looking inside text files.

Tip

Why the caveat "text files" rather than "all files"? Because since Unix treats everything as a file, it'll let you "look at" image data, executable programs, even the actual bits of the directory structure itself. None of those are useful, and while there's a program called strings that helps you snoop around in these datafiles, it's not at all commonly used in the world of Mac OS X and Terminal.

cat

The most rudimentary of the programs that let you look inside a file is called cat, not for any sort of feline, but because that's short for concatenate, a fancy word for "put a bunch of stuff together." The cat command is useful for peeking at short files, but because it doesn't care how long the file is or how big your Terminal window is set, using cat to view a long file results in the top lines scrolling right off before you can even read them.

At its most basic form, you list one or more files, and cat displays their contents to the screen:

$ cd /etc
$ cat notify.conf
#
# Notification Center configuration file
#

reserve com.apple.system. 0 0 rwr-r-
monitor com.apple.system.timezone /etc/localtime
$

In this case, I've moved to the /etc administrative directory and used cat to display the contents of the notify.conf configuration file.

Using a wildcard pattern (shown earlier), I can look at a couple of different configuration files with a single invocation of cat:

$ cat {notify,ntp,xinetd}.conf
#
# Notification Center configuration file
#

reserve com.apple.system. 0 0 rwr-r-
monitor com.apple.system.timezone /etc/localtime
server time.apple.com minpoll 12 maxpoll 17
# man xinetd.conf for more information

defaults
{
        instances               = 60
        log_type                = SYSLOG daemon
        log_on_success          = HOST PID
        log_on_failure          = HOST
        cps                     = 25 30
}

includedir /etc/xinetd.d
$

One serious drawback with using cat to view more than one file in this manner should be obvious: there's no indication of where one file ends and the next begins. The listing above is actually three different files all just dumped to the screen.

There are a couple of useful options for the cat command, most notably -n to add line numbers and -v, which ensures that everything displayed is printable (though not necessarily readable).

The split between files is more obvious when the -n option adds line numbers to the output, for example:

$ cat -n {notify,ntp,xinetd}.conf
     1  #
     2  # Notification Center configuration file
     3  #
     4
     5  reserve com.apple.system. 0 0 rwr-r-
     6  monitor com.apple.system.timezone /etc/localtime
     1  server time.apple.com minpoll 12 maxpoll 17
     1  # man xinetd.conf for more information
     2
     3  defaults
     4  {
     5          instances               = 60
     6          log_type                = SYSLOG daemon
     7          log_on_success          = HOST PID
     8          log_on_failure          = HOST
     9          cps                     = 25 30
    10  }
    11
    12  includedir /etc/xinetd.d

Here, you can see that the line numbers for each file are printed to the left of the file's contents. So, to find out where a file begins, just look for the number 1, as that's the first line of a file. This output shows us that notify.conf is six lines long, ntp.conf only has one line, and xinetd.conf is 12 lines long.

less

If you want to "read" a long plain-text file in a Terminal window, you can use the less command to display one "page" (a Terminal window filled from top to bottom) of text at a time.

If you don't like less, you can use a program named more. In fact, the name less is a play on the name of more, which came first (but less has more features than more). Here's a Mac OS X secret, though: more is less. Really. The more utility is actually the very same program—just with a different name—which gives it a different default behavior. The ls command shows the truth:

$ ls -l /usr/bin/more /usr/bin/less
-rwxr-xr-x   2 root  wheel  119128 Dec  2 16:26 /usr/bin/less
-rwxr-xr-x   2 root  wheel  119128 Dec  2 16:26 /usr/bin/more

Rather than get confused between the two, I'll just stick with less. The syntax for less is:

less options files

less lets you move forward or backward in the files that you're viewing by any number of pages or lines; you can also move back and forth between two or more files specified on the command line. When you invoke less, the first "page" of the file appears. A prompt appears at the bottom of the Terminal window, as in the following example:

$ less ch03
A file is the unit of storage in Unix, as in most other systems.
A file can hold anything: text (a report you're writing,
 .
 .
 .
:

The basic less prompt is a colon (:); although, for the first screen, less displays the file's name as a prompt. The cursor sits to the right of this prompt as a signal for you to enter a less command to tell less what to do. To quit, type q.

Like almost everything about less, the prompt can be customized. For example, using the -M starting flag on the less command line makes the prompt show the filename and your position in the file (as a percentage) at the end of each page.

Tip

If you want this to happen every time you use less, you can set the LESS environment variable to M (without a dash) in your shell setup file. See Chapter 2 for details.

You can set or unset most options temporarily from the less prompt. For instance, if you have the short less prompt (a colon), you can enter -M while less is running. less responds Long prompt (press Return), and for the rest of the session, less prompts with the filename, line number, and percentage of the file viewed.

To display the less commands and options available on your system, press h (for "help") while less is running. Table 4-2 lists some simple (but quite useful) commands.

Table 4-2. Useful less commands

Command

Description

Command

Description

Spacebar

Display next page

v

Starts the vi editor

Return

Display next line

Control-L

Redisplay current page

n f

Move forward n lines

h

Help

b

Move backward one page

:n

Go to next file on command line

n b

Move backward n lines

:p

Go back to previous file on command line

/ word

Search forward for word

q

Quit less

? word

Search backward for word

  

I quite commonly use the / word search notation, for instance, when using the man command, which uses less behind the scenes to display information one page at a time. For example, instead of flipping through bash's manpage for information on file completion, typing /file completion at the colon prompt while reading the bash manpage lets you skip straight to what you seek. Gone too far? Use b to go back to the previous page.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required