Previous | Table of Contents | Next |
The ability to list files is very important, but shell scripts also need to be able to view the contents of a file.
To view the content of a file, use the cat (short for concatenate) command. Its syntax is as follows:
cat files
Here files are the names of the files that you want to view. For example,
$ cat hosts
prints out the contents of a file called hosts:
127.0.0.1 localhost loopback 10.8.11.2 kanchi.bosland.us kanchi 10.8.11.9 kashi.bosland.us kashi 128.32.43.52 soda.berkeley.edu soda
You can specify more than one file as follows:
$ cat hosts users
If the users file contains a list of users, this produces the following output:
127.0.0.1 localhost loopback 10.8.11.2 kanchi.bosland.us kanchi 10.8.11.9 kashi.bosland.us kashi 128.32.43.52 soda.berkeley.edu soda ranga sveerara vathsa amma
Numbering Lines
The cat command also understands several options. One of these is the -n option, which numbers the output lines. You can use it as follows:
$ cat -n hosts
This produces the output
1 127.0.0.1 localhost loopback 2 10.8.11.2 kanchi.bosland.us kanchi 3 10.8.11.9 kashi.bosland.us kashi 4 128.32.43.52 soda.berkeley.edu soda 5
The numbered output shows us that the last line in this file is blank. You can ask cat to skip numbering blank lines using the -b option:
$ cat -b hosts
In this case the output looks like the following:
1 127.0.0.1 localhost loopback 2 10.8.11.2 kanchi.bosland.us kanchi 3 10.8.11.9 kashi.bosland.us kashi 4 128.32.43.52 soda.berkeley.edu soda
Although the blank line is still there, it is no longer numbered.
Now that you know how to view the contents of a file, look at how to get some information about the contents.
You can use the wc command to get a count of the total number of lines, words, and characters contained in a file. The basic syntax of this command is
wc [options] files
Here options are one or more of the options given in Table 3.1 and files are the files you want examined.
If no options are specified, the output contains a summary of the number of lines, words, and characters. For example, the command
$ wc .rhosts
produces the following output for my .rhosts file:
7 14 179 .rhosts
The first number, in this case 7, is the number of lines in the file. The second number, in this case 14, is the number of words in the file. The third number, in this case 179, is the number of characters in the file. Finally, the filename is listed. The filename is important if more than one file is specified.
If you specify more than one file, wc gives the individual counts along with a total. For example, the command
$ wc .rhosts .profile
produces the following output:
7 14 179 .rhosts 133 405 2908 .profile 140 419 3087 total
You can also use wc to get the individual counts as shown in the next sections. The options covered in these sections are given in Table 3.1.
Option | Description |
---|---|
-l | Counts the number of lines |
-w | Counts the number of words |
-m or -c | Counts the number of characters |
The -m option is available on Solaris and HP-UX. It is not available on Linux. On Linux systems, you need to use the -c option instead.
Number of Lines
To count the number of lines, use the -l (l as in lines) option. For example, the command
$ wc -l .profileproduces the output
133 .profile
Number of Words
To count the number of words in a file, use the -w (w as in words) option. For example, the command
$ wc -w .rhostsproduces the output
14 .rhostswhich is what you expected.
Number of Characters
To count the number of characters, use either the -m option or the -c option. As mentioned, the -m option should be used on Solaris and HP-UX. The -c option should be used on Linux systems.
For example, the command
$ wc -m .profileproduces the output
2908 .profileIn Linux or GNU, the equivalent command is
$ wc -c .profile
Combining Options
Like the ls command, the options to wc can be grouped together and given in any order.
For example, if you wanted a count of the number of words and characters in the file test_results you can use any of the following commands:
$ wc -w -m test_results $ wc -wm test_results $ wc -mw test_results
The output from each of these commands is identical:
606 3768 test_results
The output lists the words in the files first, the number of characters in the file, and the name of the file.
In this case, there are 606 words and 3,768 characters in the file test_results.
In the preceding sections, you looked at listing files and viewing their content. In this section you look at the following methods of manipulating files:
Previous | Table of Contents | Next |