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.

Tip

  • 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 display the command's man page.


To list a file's contents

Type cat file (for example, cat example.rtf) and press . cat lists the entire file in the Terminal window without stopping (Figure 28).

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.


Tips

  • 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 . The first page of the file appears in the Terminal window (Figure 29). The last line tells you the name of the file.

Figure 29. The less command in action.


2.

Use one of the following keystrokes:

  • Press to advance one screen.

  • Press to advance one line.

  • Press to advance one half screen.

3.

Repeat step 2 to view the entire file.

or

Press to return to the shell prompt.

Tips

  • When you reach the end of the file, you must press to return to the shell prompt.

  • You can use the -m option to display a more instructive prompt at the bottom of the screen (Figure 30).

    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?


  • 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.


To show the first lines of a file

Type head [-n count] file and press , where 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).

Figure 31. The head command displays the first bunch of lines in a file…


Tips

  • 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 , where 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).

Figure 32. …and the tail command displays the last bunch.


Tips

  • 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 . The number of lines, words, and characters (or bytes) in the file you specified is displayed in the Terminal window (Figure 33).

Figure 33. The wc command shows the number of lines, words, and characters in a file.


Tip

  • 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).




Visual QuickStart Guide. Mac OS X 10. 4 Tiger
Mac Os X 10.4 Tiger (Visual Quickstart Guides)
ISBN: 0321423372
EAN: 2147483647
Year: 2003
Pages: 301
Authors: Maria Langer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net