NEXT UP previous
Next: On-Line Help

Manipulating Files

Very often, you will want to examine the contents of a text file. You have already seen one method for doing this using the cat command. The word cat is an abbreviation of the word concatenate, which means to join together. The way in which the cat command achieves this joining is by allowing you to specify a list of file names as parameters which it will list one after the other without a break:

	$ cat /etc/passwd /etc/motd

The problem with this is that if the contents of the files you list to the screen are longer than one screenful, then the whole thing just scrolls past too fast to read and you end up only seeing the last screen full at the end of the listing. What you require is a command which lists files like cat but which will insert pauses at the end of each screen full. The usual command to do this is called more:

	$ more /etc/passwd /etc/motd

When more pauses at the end of a page there are several characters you can type to tell it what to do next:

Another command that works like more but which provides a whole host of facilities in addition to those available with more is (amusingly) called less.

In the situation where you have a file which has new text written to the end of it periodically, you may want to be able to see just the most recent additions to the file without having to page through it from the beginning, especially if it grows to be a large file. The command to do this is called tail because it just prints the tail end of a file. By default, tail prints the last ten lines of a file, though this can be changed with the -n command line switch:

	$ tail -n 4 /etc/passwd
	user: off :100:50::/home/user:/bin/sh 
	ftp:*:404:1::/home/ftp:/bin/false
	pc:fjKppCZxEvouc:500:500:/usr1/pc:/bin/bash
	carey:Ytia4ffkG2rO2:501:500::/usr1/carey:/bin/bash

In fact, the '-n 4' can be abbreviated just to '-4' and it will still perform the same function.

Another useful facility is the ability to count the number of lines, words and characters in a file. This can be done with the word count (wc) command:

	$ wc /etc/passwd
		21	42	775 /etc/passwd

The three values 21, 42 and 775 are the number of lines, words and characters respectively, in the file /etc/passwd. In this case, 21 lines shows that there are 21 account login names set up on this machine. You might have expected there to be somewhat more than 42 individual words in the password file but this is just because of the rather simple definition that wc uses for a word. It has nothing to do with looking up words in dictionaries, but is just taken to be any sequence of characters separated by spaces, tabs, newlines etc.

Printing out all three numbers is the default action performed by wc. You can obtain just the lines, words or character counts by using one of the command line switches -i, -w or -c:

	$ wc -i /etc/passwd
		21 /etc/passwd
	$ wc -w /etc/passwd
		42 /etc/passwd
	$ wc -c /etc/passwd
		775 /etc/passwd

It is also possible to use the wc command without giving a file name. In this case, the wc command will count the lines, words and characters in any text entered directly at the keyboard.

The only problem with entering text from the keyboard comes when you wish to signal to wc that you have finished typing and now want to see the counts. The way to do this is to type the end-of-file (EOF) character (Ctrl-d, remember) at the start of a line on its own. When you press Ctrl-d, the wc command will get exactly the same end-of-file indication that it would get if it were reading the information from a file and it reached the end.


NEXT UP previous
Next: On-Line Help