< Day Day Up > |
Examining File ContentsMoving 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, lessNow 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.
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.
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.
Looking at Portions of the Contents of Files: head, tailSometimes 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.
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.
|
< Day Day Up > |