Unix offers a few tools for examining the contents of files:
cat (concatenate) lists one or more files to the Terminal window.
less outputs files in page-size chunks, enabling you to view the contents of large files one screen at a time.
head displays the first lines of a file.
tail displays the last lines of a file.
wc displays a count of the number of lines, words, and characters in a file.
To learn more about these commands, check out their man pages. Type man cat, man less, man head, man tail, or man wc and press
Type cat
file … (for example, cat example.rtf) and press
cat command is used to view the contents of an RTF file. The first few lines of the filewhich you wouldn t see when viewing the file with an RTF-compatible word processor (such as TextEdit)are formatting codes.
Do not use cat to list binary executable files (that is, any non-text file, such as an application). Because they contain many nonprintable characters, they could cause Terminal to act strangely. If this happens, close the Terminal window and open a new one.
If you specify more than one file, cat lists them one after another without any indication that it has finished one file and started another one.
I explain how to use the cat command and output redirection to combine multiple files and output them to a new file later in this chapter.
If you use cat to list a long file, Terminal may not be able to store all of the lines. You may prefer to use the more command to output the file in page-sized chunks. You can learn more about the more command by typing man more.
1. | Type less file (for example, less Notes.txt) and press Figure 29. Theless command in action. |
2. | Use one of the following keystrokes: Press Press Press |
3. | Repeat step 2 to view the entire file. or Press |
When you reach the end of the file, you must press
You can use the -m option to display a more instructive prompt at the bottom of the screen (Figure 30 ).
-m option was used with the
less command. See how the prompt at the bottom of the page changes?
You can also use wildcard characters to specify multiple files. When you reach the end of each file, tell less to proceed to the next file by typing :n. Less will display the file names at the start of each file.
Like the man command discussed earlier in this chapter, the less command is a
pager . A pager displays information one screen at a time, enabling you to page through it.
Type head [-n
count ]
file … and press
count is the number of lines at the beginning of the file that you want to display. For example, head -n 15 sample.txt displays the first 15 lines of the file named
sample.txt (Figure 31 ).
head command displays the first bunch of lines in a file…
If you omit the -n
count operand, head displays the first ten lines of the file.
You can specify multiple files. If you do, head displays the file names at the start of each file.
Type tail [-n
count ]
file … and press
count is the number of lines at the end of the file that you want to display. For example, tail -n 15 sample.txt displays the last 15 lines of the file named
sample.txt (Figure 32 ).
tail command displays the last bunch.
If you omit the -n
count operand, tail displays the last ten lines of the file.
You can specify multiple files. If you do, tail displays the file names at the start of each file.
The -f option (for example tail -f log.txt) displays the last lines of the file but prevents the tail command from terminating. Instead, tail waits for the file to grow. As new lines are added to the file, tail immediately displays them. You may find this useful if you want to watch a log file grow and see the latest entries as they are added. You may also use it to watch an error log file when you are debugging a program. You cannot use the -f option if you specify multiple files; to monitor multiple files with the tail command, open multiple Terminal windows.
Type wc
file ... (for example, wc Notes.txt), and press
wc command shows the number of lines, words, and characters in a file.
You can use any combination of options for the wc command:
-c displays the number of characters
-w displays the number of words
-l displays the number of lines
With no options, wc displays all three pieces of information in this order: lines, words, characters, file name (Figure 33 ).