Viewing File Contents
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

To list a file's contents
Type cat file … (for example, cat example.rtf) and press

Figure 28. In this example, the
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.
To page through the contents of a file
1. | Type less file (for example, less Notes.txt) and press ![]() Figure 29. Theless command in action.![]() |
2. | Use one of the following keystrokes:
![]() ![]() ![]() |
3. | Repeat step 2 to view the entire file.or Press ![]() |
- When you reach the end of the file, you must press

Figure 30. In this example, the
-m option was used with the less command. See how the prompt at the bottom of the page changes?
To show the first lines of a file
Type head [-n count ] file … and press

Figure 31. The
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.
To show the last lines of a file
Type tail [-n count ] file … and press

Figure 32. …and the
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.
To count the lines, words, & characters in a file
Type wc file ... (for example, wc Notes.txt), and press

Figure 33. The
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 linesWith no options, wc displays all three pieces of information in this order: lines, words, characters, file name (Figure 33 ).