Because Unix uses text files for so many things (including system configuration, source code, log files, and documentation), you will frequently want to view the contents of text files. Unix provides many tools to do this, and we've described the more common ones here. To learn how to edit text files, see Chapter 6, "Editing and Printing Files."
All the tools shown here apply not only to viewing text files but also to seeing the output of other commands; that is, you can pipe the output of commands into the tools shown here. (Review the Chapter 2 section "Creating Pipelines of Commands.")
The two most common ways to view text files are with the commands cat and less . We have seen these commands in earlier chapters, but we will go into more detail about them here.
The cat command (short for concatenate ) combines all of its input and sends it without any pauses to stdout (review the Chapter 2 section "About Standard Input and Output" for more on stdout ).
Remember that the less command is a pager , a program that displays one page or screen of a file at a time, waiting for your command to show the next page.
Using a pager
The two most common pager programs are called more and less . The less program is an advanced version of the more program, so that is the one we describe here. (The more program was invented first, and it gets its name from the fact that you have to keep asking it to show you more of a file. The less command's name is the sort of recursive word game that Unix programmers love: less is an improved version of more ; thus less is more.)
To view a file one screen at a time:
If your Terminal window is 24 lines high, then each "page" is 23 lines longthat is, one line less than your Terminal height. less uses the bottom line of the window to display status information and to accept some commands from you.
If you want to go forward one page, press the
If you want to go back one page, press
If you want to skip forward to the next occurrence of string ,
and then press
If you want skip back to the prior occurrence of string ,
and then press
If you want to go directly to line 23, type 23G (that's an uppercase G ).
If you want to go directly to the end of the file, type G (that's an uppercase G , for go ).
If you want to exit from less , type q (for quit ).
Pipe the output of other commands into less so that you can see their output one page at a time. For example,
grep Copyright *.c
searches all the .c files in the current directory for the string Copyright (see Chapter 4 or man grep for details on grep ).
The resulting output could be very long, so you pipe it through less :
grep Copyright *.c less
Sometimes you want to see an entire file without pausing. Perhaps you know the file is very short, or you want to use the Terminal window's scroll bars to move up and down. You might want to select the text with the mouse to copy or print it.
Another reason to see an entire file is to join two or more files together. Unix provides the cat tool for this purpose.
To see an entire file without pausing:
- cat path
The cat command gets its name from concatenate, and in fact that is what it doesit concatenates (or combines) all the files in its argument list and sends them to its output. For example,
cat file1 file2 file3
results in the contents of all three files appearing on your screen without a break.
Use cat to concatenate several files into one new file:
cat file1 file2 file3 > newfile
Use the -n option to number lines. For example:
cat -n script.pl
Figure 5.18 shows an example of using cat -n to see a file with line numbers . (The example uses the script you created in Chapter 2.)
Use the -s option to "squeeze out" extra blank lines, resulting in single- spaced output. For example:
cat -s file1 file2 > newfile
As always, see the man page for more options: man cat .
Figure 5.18. Using cat -n adds line numbers to the output of cat .
localhost:~ vanilla$ cat -n ~/bin/status.sh 1 #!/bin/sh 2 # This is a comment. Comments are good. 3 # This is my first shell script. 4 echo "System Status Report" 5 date 6 echo -n "System uptime and load:" ; uptime 7 echo -n "Operating System: " ; sysctl -n kern.ostype 8 echo -n "OS Version: " ; sysctl -n kern.osrelease 9 echo -n "OS Revision number: " ; sysctl -n kern.osrevision 10 echo -n "Hostname: " ; sysctl -n kern.hostname 11 12 bytes=`sysctl -n hw.physmem` 13 megabytes=`expr $bytes / 1024 / 1024` 14 echo "Physical memory installed (megabytes): $megabytes" localhost:~ vanilla$
Seeing just the beginning or end of a file
Sometimes you want to see just the first (or last) few lines of a file. You might want to confirm that the file contains what you expect, or to see the last lines added to a log file.
The Unix commands head and tail show you the beginning or end (respectively) of their input.
To view just the beginning of a file:
- head path
The head command shows you the first 10 lines of a file.
You can control the number of lines to display by giving the number as an option. For example,
head -15 /etc/rc
displays the first 15 lines of the system-configuration file /etc/rc ( Figure 5.19 ).
Figure 5.19. The head command shows the specified number of lines from the start of its input.
localhost:~ vanilla$ head -15 /etc/rc #!/bin/sh # Copyright 1997-2004 Apple Computer, Inc. . /etc/rc.common export -n SafeBoot export -n VerboseFlag export -n FsckSlash export -n NetBoot if [ -d /System/Installation -a -f /etc/rc.cdrom ]; then /etc/rc.cdrom multiuser # We shouldn't get here; CDIS should reboot the machine when done echo "CD-ROM boot procedure complete" halt localhost:~ vanilla$
To view just the end of a file:
- tail path
The tail command works just like the head command, but it shows you the end of a file. For example,
tail -100 /var/log/mail.log
displays the last 100 lines of the system mail log.
The head and tail commands can both be used on multiple files; add as many filenames as you need on the command line. For example:
tail file1 file2 or head *.html
The output will have each file's name. Remember that you can use full or relative paths wherever you are using filenames.
We've mentioned system log files several times because Unix systems keep a variety of log files to record system events (startup and shutdown, e-mail being sent or received, and Web pages served by a Web server, to name a few).
Checking the addition of new lines to a log file is very useful. You might be debugging a piece of software or watching to see the effect of some change to your network. You could continuously run the tail command, but it is far more useful to tell the tail command to simply keep showing you any new lines as they are added.
To view the end of a file while it is growing:
Use the -f option to tail .
tail -f /var/log/system.log
shows all lines being added to the main system log file as they are added.
The -f (for follow ) option shows you the end of the file and keeps displaying new lines as they are added. Unix experts call this "tailing a file."
When you want to stop tail -f
More About less
The less program has a large number of options and an even larger number of commands for moving around inside a file. Type man less at a shell prompt to read the Unix manual entry on less for a complete (if rather technical) description of its capabilities and features.
Keep an extra Terminal window open when you need to run tail -f . That way, you can see the file you are tailing in one window and do your work in the other one.
Pipe the output of other commands through head or tail to see just the start or end of the output. For example,
shows just the last ten lines of output from the last command (which shows all log-ins, crashes, and reboots in the current month; see man last ).
More About Hidden Files
Mac OS X uses a variety of files and directories that are hidden from casual view. In most cases you will have no reason to mess with these files, but as you get further in Unix, you will want to know what they are and why they are there.
A good summary of the hidden files and folders used by Mac OS X is at "Mac OS X Hidden Files & Directories" (Westwind Computing; www.westwind.com/reference/OS-X/invisibles.html).
Also, see Chapter 7 for a discussion of how to make some hidden files appear in the Finder.