- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Part I: Introduction to Mac OS X
- Chapter 1. Mac OS X Component Architecture
- Chapter 2. Installing Mac OS X
- Chapter 3. Mac OS X Basics
- Chapter 4. The Finder: Working with Files and Applications
- Chapter 5. Running Classic Mac OS Applications
- Part II: Inside Mac OS X
- Chapter 6. Native Utilities and Applications
- Chapter 7. Internet Communications
- Chapter 8. Installing Third-Party Applications
- Part III: User-Level OS X Configuration
- Chapter 9. Network Setup
- Chapter 10. Printer and Font Management
- Chapter 11. Additional System Components
- Part IV: Introduction to BSD Applications
- Chapter 12. Introducing the BSD Subsystem
- Chapter 13. Common Unix Shell Commands: File Operations
- Part V: Advanced Command-Line Concepts
- Chapter 14. Advanced Shell Concepts and Commands
- Chapter 15. Command-Line Applications and Application Suites
- Chapter 16. Command-Line Software Installation
- Chapter 17. Troubleshooting Software Installs, and Compiling and Debugging Manually
- Chapter 18. Advanced Unix Shell Use: Configuration and Programming (Shell Scripting)
- Part VI: Server/Network Administration
- Chapter 19. X Window System Applications
- Chapter 20. Command-Line Configuration and Administration
- Chapter 21. AppleScript
- Chapter 22. Perl Scripting and SQL Connectivity
- Chapter 23. File and Resource Sharing with NetInfo
- Chapter 24. User Management and Machine Clustering
- Chapter 25. FTP Serving
- Chapter 26. Remote Access and Administration
- Chapter 27. Web Serving
- Part VII: Server Health
- Chapter 28. Web Programming
- Chapter 29. Creating a Mail Server
- Chapter 30. Accessing and Serving a Windows Network
- Chapter 31. Server Security and Advanced Network Configuration
- Chapter 32. System Maintenance
- Appendix A. Command-Line Reference
- Appendix B. Administration Reference
Examining File Contents
Moving around the file system, and moving files around the file system, 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 what you want to do is 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 your files 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 m y file is very short.
[localhost:~] nermal% 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 Chapter 18. 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 paged display of data.
The complete syntax and options for cat are shown in Table 13.6, the command documentation table.
Table 13.6. The Command Documentation Table 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 standard input. | |
| -n | Numbers all output lines. |
| -b | Numbers all output lines, except b or 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 (octal 0177) 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. |
| -e | Implies -v option. Displays a dollar sign ($) at the end of each line as well. |
| -t | Implies -v option. Displays tab characters as ^I as well. |
| -u | Guarantees unbuffered output. |
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:
[localhost:~] nermal% more myfile
Hi. this is nermal.
I hope you enjoyed myfile.
With a longer file, though, more pauses after a screenful:
[localhost:/var/log] joray% more system.log
Apr 23 03:15:02 localhost syslogd: restart
Apr 23 03:15:02 localhost sendmail[7423]: NOQUEUE: SYSERR(root): /etc/mail/sendmail.cf:
line 81: fileclass: cannot open /etc/mail/local-host-names: Group writable directory
Apr 23 03:15:02 localhost sendmail[7424]: NOQUEUE: SYSERR(root): /etc/mail/sendmail.cf:
line 81: fileclass: cannot open /etc/mail/local-host-names: Group writable directory
Apr 23 03:15:02 localhost CRON[7390]: (root) MAIL (mailed 108 bytes of output but got
status 0x0047 )
Apr 23 09:01:03 localhost WindowServer[56]: CGXGetSharedWindow: Invalid window -1
Apr 23 09:01:03 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine:
kCGSErrorIllegalArgument : CGSLockWindowRectBits: Invalid window
Apr 23 09:01:03 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS /ScreenSaverEngine:
kCGSErrorCannotComplete : Cannot create context device
Apr 23 09:01:03 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS /ScreenSaverEngine: kCGSErrorFailure :
Cannot initialize context RIPContext
Apr 23 10:15:46 localhost WindowServer[56]: CGXGetSharedWindow: Invalid window -1
Apr 23 10:15:46 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions/A/R
esources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine:
kCGSErrorIllegalArgument : CGSLockWindowRectBits: Invalid window
Apr 23 10:15:46 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine:
kCGSErrorCannotComplete : Cannot create context device
Apr 23 10:15:46 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine: kCGSErrorFailure :
Cannot initialize context RIPContext
Apr 23 10:17:26 localhost WindowServer[56]: CGXGetSharedWindow: Invalid window -1
Apr 23 10:17:26 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine:
kCGSErrorIllegalArgument : CGSLockWindowRectBits: Invalid window
Apr 23 10:17:26 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS /ScreenSaverEngine: kCGSErrorCannotComplet
system.log (11%)
In this display, we can see at the bottom of the screen that we are looking at a file called sy s tem.log, and that we have viewed about 11% of the file. After we are done looking at that screenful, we can press the space bar and look at the next screenful of text. Although more has many options to it, probably the most common syntax you will use is
more <filename>
The complete syntax and options for more are in Table 13.7, the command documentation table.
Table 13.7. The Command Documentation Table for more
| more | Pages through data or text files. |
more [-cdflsu] [-n] [+<linenumber>] [+/<pattern>]
<file1><file2> ...
|
|
| more pages through data a screenful at a time. When the user presses a carriage return at the More prompt at the bottom of the screen, one more line is displayed. When the user presses the space bar, another screenful of data is displayed. When more is invoked as page, each screenful is cleared before the next is displayed. | |
| -c | Draws each page by beginning at the top of the screen and erasing each line just before it draws on it. This option is ignored if the screen is unable to clear to the end of a line. |
| -d | Prompts user with Press space to continue, 'q' to quit. at the end of each screenful. Responds to illegal user input with Press 'h' for instructions. instead of ringing the bell. |
| -f | Counts logical rather than screen lines. Long lines are not folded. Useful when trying to display lines containing nonprinting characters or escape sequences. |
| -l | Does not treat ^L (form feed) as a page break. Where form feeds occur, more pauses after them, as if the screen were full. Particularly recommended if piping nroff output through ul. |
| -s | Squeezes multiple blank lines of output into one blank line of output. Useful for viewing nroff output. |
| -u | Suppresses underlining or stand-out mode, whichever the terminal is capable of displaying. |
| -n | Specifies the number of lines to use per screenful rather than the default. |
| + <linenumber> | Starts at <linenumber> . |
| +/ <pattern> | Starts two lines before the line containing the regular expression pattern <pattern> . |
| Additional options for interacting with more when it pauses ( i is an optional integer argument, defaulting to 1): | |
| i<return> | Displays i more lines. Advances one line, if i is not given. |
| i<space> | Displays i more lines. Advances another screenful if i is not given. |
| ^D | Displays 11 more lines. If i is given, scroll size is set to i . |
| d | Same as ^D. |
| i z | Same as typing <space> , except that if i is given, scroll size becomes i . |
| i s | Skips i lines and prints a screenful of lines. |
| i f | Skips i screenfuls and prints a screenful of lines. |
| i ^F | Same as i f. |
| i b | Skips back i screenfuls and prints a screenful of lines. |
| i ^B | Same as i b. |
| q | Exits. |
| Q | Exits. |
| = | Displays the current line number. |
| v | Starts the editor at the current line number, if the environment variable EDITOR is set to vi or ex. If no EDITOR is specified, vi is the default. |
| h | Displays the help menu. |
| i/< expression > | Searches for the i th occurrence of the regular expression <expre s sion> . If the input is a file rather than a pipe, and there are less than i occurrences, the file remains unchanged. Otherwise, the display advances to two lines before the line containing <expression>. |
| i n | Searches for the i th occurrence of the last regular expression entered. |
| ' | (Single quote) Goes to the point where the last search was started. If no search has been done on the file, it goes back to the beginning of the file. |
| ! <command> | Invokes a shell that executes <command> . The characters % and !, when used in the <command> , are replaced with the current filename and the previous shell command, respectively. If there is no current filename, % is not expanded. To escape expansion, use \% and \%, respectively. |
| i :n | Skips to the i th next file given in the command line, or to the last file if i is beyond range. |
| i :p | Skips to the i th previous file in the command line, or to the first file if i is beyond range. If more is in the middle of displaying a file, it goes to the beginning of the file. If more is displaying from a pipe, the bell rings. |
| :f | Displays current filename and line number. |
| :q | Exits. |
| :Q | Exits. |
| . | (Dot) Repeats the previous command. |
less is another pager. Because it does not have to read the entire file first, it starts faster. In addition, it has more backward and forward movement. Its commands are based both on more and vi, a text editor which we will examine in Chapter 15, "Command-Line Applications." Although less is frequently thought of as a command, it has enough complex functionality for the user who desires 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:
[localhost:/var/log] joray% less system.log
Apr 23 03:15:02 localhost syslogd: restart
Apr 23 03:15:02 localhost sendmail[7423]: NOQUEUE: SYSERR(root): /etc/mail/sendmail
.cf: line 81: fileclass: cannot open /etc/mail/local-host-names: Group writable directory
Apr 23 03:15:02 localhost sendmail[7424]: NOQUEUE: SYSERR(root): /etc/mail/sendmail
.cf: line 81: fileclass: cannot open /etc/mail/local-host-names: Group writable directory
Apr 23 03:15:02 localhost CRON[7390]: (root) MAIL (mailed 108 bytes of output but got
status 0x0047 )
Apr 23 09:01:03 localhost WindowServer[56]: CGXGetSharedWindow: Invalid window -1
Apr 23 09:01:03 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS /ScreenSaverEngine:
kCGSErrorIllegalArgument : CGSLockWindowRectBits: Invalid window
Apr 23 09:01:03 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine:
kCGSErrorCannotComplete : Cannot create context device
Apr 23 09:01:03 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine: kCGSErrorFailure :
Cannot initialize context RIPContext
Apr 23 10:15:46 localhost WindowServer[56]: CGXGetSharedWindow: Invalid window -1
Apr 23 10:15:46 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine:
kCGSErrorIllegalArgument : CGSLockWindowRectBits: Invalid window
Apr 23 10:15:46 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine:
kCGSErrorCannotComplete : Cannot create context device
Apr 23 10:15:46 localhost /System/Library/Frameworks/ScreenSaver.framework/ Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine: kCGSErrorFailure :
Cannot initialize context RIPContext
Apr 23 10:17:26 localhost WindowServer[56]: CGXGetSharedWindow: Invalid window -1
Apr 23 10:17:26 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine:
kCGSErrorIllegalArgument : CGSLockWindowRectBits: Invalid window
Apr 23 10:17:26 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine: kCGSErrorCannotComplet
system.log
Like more, less has paused 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 very 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 quite overwhelming, but the —help option is easy to read and organized nicely. The output from the —help option is included in Table 13.8.
Many options are available in less, and there is much that you can do after you are in less. The syntax and options for less are in Table 13.8, the command documentation table.
Table 13.8. The Command Documentation Table for less
| less | Opposite of more. |
| Pages through data or text files. | |
less -?
less --help
less -V
less --version
less [-[+]aBcCdeEfgGiImMnNqQrsSuUVwX][-b
<bufs>][-h <lines>][-j <line>]
[-k <keyfile>] [--{oO} <logfile>] [-p
<pattern>][-t <tag>] [-T <tags-
file>] [-x <tab>] [-y <lines>] [-[-z]
<lines>][+[+]<cmd>] [--]
[<file1>...]
|
|
|
|
Looking at Portions of the Contents of Files: head, tail
Sometimes, however, 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:
[localhost:/var/log] joray% head system.log
Apr 23 03:15:02 localhost syslogd: restart
Apr 23 03:15:02 localhost sendmail[7423]: NOQUEUE: SYSERR(root): /etc/mail/sendmail
.cf: line 81: fileclass: cannot open /etc/mail/local-host-names: Group writable directory
Apr 23 03:15:02 localhost sendmail[7424]: NOQUEUE: SYSERR(root): /etc/mail/sendmail
.cf: line 81: fileclass: cannot open /etc/mail/local-host-names: Group writable directory
Apr 23 03:15:02 localhost CRON[7390]: (root) MAIL (mailed 108 bytes of output but got
status 0x0047 )
Apr 23 09:01:03 localhost WindowServer[56]: CGXGetSharedWindow: Ivalid window -1
Apr 23 09:01:03 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ ScreenSaverEngine:
kCGSErrorIllegalArgument : CGSLockWindowRectBits: Invalid window
Apr 23 09:01:03 localhost /System/Library/Frameworks/ScreenSaver.framework /Versions
/A/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine:
kCGSErrorCannotComplete : Cannot create context device
Apr 23 09:01:03 localhost /System/Library/Frameworks/ScreenSaver.framework/Versions/
A/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine: kCGSErrorFailure :
Cannot initialize context RIPContext
Apr 23 10:15:46 localhost WindowServer[56]: CGXGetSharedWindow: Invalid window -1
Apr 23 10:15:46 localhost /System/Library/Frameworks/ScreenSaver.framework/Versions/
A/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine:
kCGSErrorIllegalArgument : CGSLockWindowRectBits: Invalid window
Nothing other than the first few lines of the file are displayed. Because head is not a pager, we do not see the name of the file displayed at the bottom of the screen. The complete syntax and options for head are in Table 13.9, the command documentation table.
Table 13.9. The Command Documentation Table 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. |
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:
[localhost:/var/log] joray% tail system.log
Apr 23 17:30:09 localhost sshd[807]: lastlog_perform_login: Couldn't stat /var/log
/lastlog: No such file or directory
Apr 23 17:30:09 localhost sshd[807]: lastlog_openseek: /var/log/lastlog is not a file
or directory!
Apr 23 17:30:10 localhost sshd[807]: lastlog_perform_login: Couldn't stat /var/log
/lastlog: No such file or directory
Apr 23 17:30:10 localhost sshd[807]: lastlog_openseek: /var/log/lastlog is not a file
or directory!
Apr 23 17:31:31 localhost su: joray to nermal on /dev/ttyp4
Apr 23 17:34:07 localhost su: joray to nermal on /dev/ttyp4
Apr 23 17:36:43 localhost sshd[298]: Generating new 768 bit RSA key.
Apr 23 17:36:45 localhost sshd[298]: RSA key generation complete.
Apr 23 17:42:45 localhost su: BAD SU nermal to joray on /dev/ttyp4
Apr 23 17:42:58 localhost su: nermal to joray on /dev/ttyp4
The complete syntax and options for tail are in Table 13.10, the command documentation table.
Table 13.10. The Command Documentation Table for tail
| tail | Displays the last part of a file. |
tail [-f | -F | -r] [-b <number> | -c <number> |
-n <number>] <file>
tail [-f | -F | -r] [-b <number> | -c <number> |
-n <number>]
|
|
| -f | Waits for and displays additional data that <file> receives, rather than 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. If so, tail closes the current file, opens the filename given, displays its entire contents, and waits for more data. This option is especially useful for monitoring log files that undergo rotation. |
| -r | Displays the file in reverse order, by line. The default is to display the entire file in reverse. 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 from the beginning of the input. If <number> begins with -, it refers to the number of units from the end of the input. |
Deleting Files | Next Section

Account Sign In
View your cart