Searching for Files, Directories, and More

 < Day Day Up > 

Examining File Contents

Moving around the filesystem, and moving files around the filesystem, isn't all that interesting if you can't look at what's in the files. Unix provides a number of facilities for examining the contents of files, and frequently these are more convenient to use than their graphical counterparts. BBEdit, for example, is a wonderful text editor, and it's light enough in memory footprint to load quickly. However, if you want to see whether the file you're thinking about deleting is really the file you mean to delete, and the information is readily apparent by looking at the beginning of the text, there are much more efficient ways to examine the contents from the command line than starting up a GUI program just to glance at the file.

Looking at the Contents of Files: cat, more, less

Now that you have learned a little bit about how to list and copy your files, it is time to learn how to examine the contents of your files.

cat reads files and displays their contents. In this example, you see that myfile is short.

 brezup:nermal Documents $ cat myfile Hi.  this is nermal. I hope you enjoyed myfile. 

If the file were longer, it would keep scrolling by on the screen either until you pressed Ctrl-C to break the process or the file came to an end.

It might seem odd that anyone would want a program that just dumped all the output to the terminal, with no convenient way to slow it down or page through it. However, this is part of the Unix philosophy. The cat command reads files and sends their contents to the terminal. (Actually, cat sends their contents to STDOUT, a way of connecting commands that you'll learn more about in Chapters 12 and 15. STDOUT just happens to be connected to the terminal, unless you tell the command line otherwise.) In the Unix way of doing things, it is the job of some other program to provide a paged display of data.

Table 10.13 shows the complete syntax and primary options for cat.

Table 10.13. The Syntax and Primary Options for cat

Cat

Concatenates and prints files.

cat [-nbsvetu] <file1> <file2> ...

cat [-nbsvetu] [-]

cat reads files in sequential, command-line order and writes them to standard output. A single dash represents reading the standard input, rather than an on-disk file.

-n

Numbers all output lines, starting with 1.

-b

Numbers all output lines, except blank lines.

-s

Squeezes multiple adjacent empty lines, causing single-spaced output.

-v

Displays nonprinting characters. Control characters print as ^X for Control-X; delete prints as ^?; non-ASCII characters with the high bit set are printed as M- (for meta) followed by the character for the low 7 bits.


You could also use cat to read the contents of longer files. However, the contents of your file scroll quickly. If you hope to read the contents as they appear, it would be better to use more, which also reads and displays files, but it pauses the display after a screenful.

The contents of nermal's short file look the same when viewed with more:

 brezup:nermal Documents $ more myfile Hi.  this is nermal. I hope you enjoyed myfile. 

With a longer file, though, more pauses after a screenful:

brezup:nermal Documents $ more /var/log/system.log Sep 11 16:00:54 localhost syslogd: restart Sep 11 16:00:54 localhost syslogd: kernel boot file is /mach_kernel Sep 11 16:00:54 localhost kernel: /IOFireWireSBP2LUN/com_apple_driver_LSI_FW_500 /IOSCSIPeripheralDeviceNub/IOSCSIPeripheralDeviceType00/I OBlockStorageServices/IOBlockStorageDriver/Maxtor 1394 storage Media /IOApplePartitionScheme/untitled@3 Sep 11 16:00:54 localhost kernel: BSD root: disk1s3, major 14, minor 9 Sep 11 16:00:54 localhost kernel: HFS: created HFBT on Maxtor 38 GB HD Sep 11 16:00:56 localhost kernel: Jettisoning kernel linker. . . . Sep 11 16:01:13 localhost kernel: obtaining ID Sep 11 16:01:13 localhost kernel: from Registry Sep 11 16:01:13 localhost kernel: ATIRage128: using AGP Sep 11 16:01:15 localhost lookupd[136]: lookupd (version 322) starting - Thu Sep 11 16:01 :15 2003 Sep 11 16:01:17 localhost diskarbitrationd[106]: disk1s3 hfs E14C9FFC-2328-3ACB-98DC-EA27AD8B1880 Maxtor 38 GB HD / Sep 11 16:01:17 localhost SystemStarter: Welcome to Macintosh. Sep 11 16:01:17 localhost kernel: UniNEnet: Ethernet address 00:30:65:aa:37:ae Sep 11 16:01:17 localhost kernel: IOFireWireIP: FireWire address 00:30:65:ff:fe:aa:37:ae /var/log/system.log (3%)

In this display, we can see at the bottom of the screen that we are looking at a file called system.log, and that we have viewed about 3% of the file. After we've finished looking at that screenful, we can press the spacebar and look at the next screenful of text. The most common syntax you will use for more is

 more <filename> 

Another way of looking at files with a paged view is by using the command less. less started off live as a more advanced pager that was named as a Unix-style pun on more. Now, more is actually just an alternative functionality of the less executable, and traditional more isn't included as an independent application. more and less have traditionally had several differences in their behaviors, and the new less appliction preserves these differences when it's invoked under each name.

For example, more TRaditionally scrolls data off the top of the screen when you press the spacebar to move to the next page, and less traditionaly erases the screen and repaints it with the new page of data. This difference might not seem significant at first, but it means that in more, you can scroll back to something that's earlier in the file by using Terminal's scrollbar. In less, on the other hand, things don't accumulate in the scroll buffer. If your terminal type supports it, less erases the screen back to what it was before you ran less, when it exits, leaving no mess from the pager in your Terminal window or buffer at all. These modes of operation both have advantages and disadvantages. more's output accumulates as part of a Terminal's history, whereas the output from less is displayed only temporarily, and leaves you back at a clean Terminal with all of your previous activity right where it previously was before you ran the command.

Another difference is that more TRaditionally exits after hitting the bottom of the file, whereas less TRaditionally remains in the pager, enabliing you to issue in-pager searches and move throughout the file even after it's hit bottom. Both of these behaviors are annoying, when you're expecting the other!

less's commands are based both on traditional more and vi, a text editor we introduce in Chapter 13. Although less is frequently thought of as a command, it has enough complex functionality for the user who wants it that less might be better called an application. The fact that it is most frequently used for its command-like capabilities leads to its inclusion here.

The appearance of less output is similar to that from more:

brezup:nermal Documents $ less system.log Sep 11 16:00:54 localhost syslogd: restart Sep 11 16:00:54 localhost syslogd: kernel boot file is /mach_kernel Sep 11 16:00:54 localhost kernel: /IOFireWireSBP2LUN/com_apple_driver_LSI_FW_500 /IOSCSIPeripheralDeviceNub/IOSCSIPeripheralDeviceType00/I OBlockStorageServices/IOBlockStorageDriver/Maxtor 1394 storage Media /IOApplePartitionScheme/untitled@3 Sep 11 16:00:54 localhost kernel: BSD root: disk1s3, major 14, minor 9 Sep 11 16:00:54 localhost kernel: HFS: created HFBT on Maxtor 38 GB HD Sep 11 16:00:56 localhost kernel: Jettisoning kernel linker. . . . Sep 11 16:01:13 localhost kernel: obtaining ID Sep 11 16:01:13 localhost kernel: from Registry Sep 11 16:01:13 localhost kernel: ATIRage128: using AGP Sep 11 16:01:15 localhost lookupd[136]: lookupd (version 322) starting - Thu Sep 11 16:01 :15 2003 Sep 11 16:01:17 localhost diskarbitrationd[106]: disk1s3 hfs E14C9FFC-2328-3ACB-98DC-EA27AD8B1880 Maxtor 38 GB HD / Sep 11 16:01:17 localhost SystemStarter: Welcome to Macintosh. Sep 11 16:01:17 localhost kernel: UniNEnet: Ethernet address 00:30:65:aa:37:ae Sep 11 16:01:17 localhost kernel: IOFireWireIP: FireWire address 00:30:65:ff:fe:aa:37:ae

Like more, less pauses after a screenful. At the bottom, we also see that the file is called system.log, but it is not displaying the percentage of the file that we have examined.

The most common syntax you will use for less is

 less <filename> 

less is powerful. The most important thing to remember about less is how to invoke help, which can be done by issuing either less -? or less --help. Depending on how your shell interprets a question mark, you might have to try -\? or "-\?" for help. The man page is overwhelming, but the --help option is easy to read and organized nicely. Some of the highlights from the output of the --help option are included in Table 10.14.

Table 10.14. The Command Documentation Table for less

less

Opposite of more.

Pages through data or text files.

less -?

less --help

less -V

less --version

less [-[+]aBcCdeEfFgGiIJmMnNqQRrsSuUVvwX~][-b <space>][-h <lines>][-j <line>] [-k <keyfile>] [--{oO} <logfile>] [-p <pattern>][-t <tag>] [-T <tagsfile>] [-x <tab,...>] [-y <lines>] [-[-z] <lines>][-# <shift>][+[+]<cmd>] [--] [<file1>...]

Summary of less Commands

Commands marked with * may be preceded by a number, N.

Notes in parentheses indicate the behavior if N is given.

h

H

     

Display this help.

q

:q

Q

:Q

ZZ

  

Exit.

MOVING

e

^E

j

^N

CR

 

*

Forward one line (or N lines).

y

^Y

k

^K

^P

 

*

Backward one line (or N lines).

f

^F

^V

SPACE

  

*

Forward one window (or N lines).

b

^B

ESC-v

   

*

Backward one window (or N lines).

z

     

*

Forward one window (and set window to N).

w

     

*

Backward one window (and set window to N).

ESC-SPACE

     

*

Forward one window, but don't stop at end-of- file.

d

^D

    

*

Forward one half-window (and set half-window to N).

u

^U

    

*

Backward one half-window (and set half-window to N).

ESC-(

 

RightArrow

   

*

Left eight character positions (or N positions).

ESC-)

 

LeftArrow

   

*

Right eight character positions (or N positions).

F

      

Forward forever; like "tail -f".

r

^R

^L

    

Repaint screen.

R

      

Repaint screen, discarding buffered input.

Default window is the screen height.

Default half-window is half of the screen height.

SEARCHING

/pattern

     

*

Search forward for (N-th) matching line.

?pattern

     

*

Search backward for (N-th) matching line.

n

     

*

Repeat previous search (for N-th occurrence).

N

     

*

Repeat previous search in reverse direction.

JUMPING

g

<

ESC-<

   

*

Go to first line in file (or line N).

G

>

ESC->

   

*

Go to last line in file (or line N).

p

%

    

*

Go to beginning of file (or N percent into file).


Many options are available in less, and there is much that you can do after you are in less. Table 10.14 shows the syntax for less, as well as some basic information on movement and pattern searching in less.

NOTE

Those who find the technical underpinnings of the command line to be interesting (c'mon, admit it the stuff you can do in here is fascinating!), should note that when they run less or more on recent versions of Mac OS X, they're actually running the exact same program. The command itself looks to see which name was used to invoke it, and assumes the traditional behavior of whichever is appropriate when it runs. This would be kind of like having icons in the Finder for both Mail and Safari, but both of them being aliases for a single application that can act as either a mail reader or a web browser they do share a lot of functionality, so why not disk space as well?


Looking at Portions of the Contents of Files: head, tail

Sometimes you need to see only a portion of a file, rather than the entire contents. To see only portions of a file, use either head or tail. As the names suggest, head displays the first few lines of a file, whereas tail displays the last few lines of a file.

Let's look at the first few lines of system.log:

brezup:joray log $ head system.log Sep 11 16:00:54 localhost syslogd: restart Sep 11 16:00:54 localhost syslogd: kernel boot file is /mach_kernel Sep 11 16:00:54 localhost kernel: /IOFireWireSBP2LUN/com_apple_driver_LSI_FW_500 /IOSCSIPeripheralDeviceNub/IOSCSIPeripheralDeviceType00/IOBlockStorageServices /IOBlockStorageDriver/Maxtor 1394 storage Media/IOApplePartitionScheme/untitled@3 Sep 11 16:00:54 localhost kernel: BSD root: disk1s3, major 14, minor 9 Sep 11 16:00:54 localhost kernel: HFS: created HFBT on Maxtor 38 GB HD Sep 11 16:00:56 localhost kernel: Jettisoning kernel linker. Sep 11 16:01:02 localhost kextd[86]: registering service "com.apple.KernelExtensionServer" Sep 11 16:01:03 localhost kernel: Resetting IOCatalogue. Sep 11 16:01:04 localhost kernel: Matching service count = 0 Sep 11 16:01:08 localhost kernel: AppleRS232Serial: 0 0 AppleRS232Serial::start - returning false early, Connector or machine incorrect brezup:joray log $

Nothing other than the first few lines of the file is displayed. Because head is not a pager, we do not see the name of the file displayed at the bottom of the screen, and the system prompt returns immediately when head is finished displaying its output. Table 10.15 shows the complete syntax and options for head.

Table 10.15. The Syntax and Options for head

head

Displays the first lines of a file.

head [-n <number>] <file1> <file2> ...

head [-n <number>]

-n <number>

Displays the first <number> of lines. If n is not specified, the default is 10.

If more than one file is specified, the output for each file is preceded by a short header indicating the file number that is about to be displayed.


tail behaves in the same way as head, except that only the last few lines of a file are displayed, as you see in this sample. The line Sep 16 23:58:42 localhost xinetd[329]: START: ssh pid=1010 from=192.168.1.4 is the last one in the file:

brezup:joray log $ tail system.log Sep 16 11:31:32 localhost sshd[891]: Accepted password for sageray from 192.168.1.4 port 2260 ssh2 Sep 16 22:58:32 localhost xinetd[329]: service ssh, IPV6_ADDRFORM setsockopt() failed: Protocol not available (errno = 42) Sep 16 22:58:32 localhost xinetd[329]: START: ssh pid=906 from=192.168.1.4 Sep 16 22:58:52 localhost sshd[906]: Accepted password for sageray from 192.168.1.4 port 2272 ssh2 Sep 16 23:54:18 localhost xinetd[329]: service ssh, IPV6_ADDRFORM setsockopt() failed: Protocol not available (errno = 42) Sep 16 23:54:18 localhost xinetd[329]: START: ssh pid=1004 from=192.168.1.4 Sep 16 23:54:25 localhost sshd[1004]: Accepted password for sageray from 192.168.1.4 port 2274 ssh2 Sep 16 23:58:42 localhost xinetd[329]: service ssh, IPV6_ADDRFORM setsockopt() failed: Protocol not available (errno = 42) Sep 16 23:58:42 localhost xinetd[329]: START: ssh pid=1010 from=192.168.1.4 ''

Table 10.16 shows the complete syntax and options for tail.

Table 10.16. The Syntax and Options for tail

tail

Displays the last part of a file.

tail [-f | -F | -r] [-b <number> | -c <number> | -n <number>] <file> ...

-f

Waits for and displays additional data that <file> receives, instead of stopping at the end of the file.

-F

Similar to -f, except that every five seconds, tail checks whether <file> has been shortened or moved.

-r

Displays the file in reverse order, by line. This option also modifies the -b, -c, and -n options to specify the number of units to be displayed, rather than the number of units to display from the beginning or end of the input.

-b <number>

Specifies location in number of 512-byte blocks.

-c <number>

Specifies location in number of bytes.

-n <number>

Specifies location in number of lines.

If <number> begins with +, it refers to the number of units (512-byte blocks, bytes, or lines) from the beginning of the input. If <number> begins with - or no explicit sign, it refers to the number of units from the end of the input.

If more than one file is specified, the output for each file is preceded by a short header indicating the file number that is about to be displayed.


     < Day Day Up > 


    Mac OS X Tiger Unleashed
    Mac OS X Tiger Unleashed
    ISBN: 0672327465
    EAN: 2147483647
    Year: 2005
    Pages: 251

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