Thursday, August 09, 2001

Begining and end of a file

Some times you might want to see only the first or last few lines of a file. The commands head and tail

allows you to do precisely that.

To see the first 10 lines of a file do:

head file-name

If you want any other number of lines, say 15, do as in this example:

head -15 file-name

In case you prefer to see the first 30 bytes of a file you can do

head -c 30 file-name

You can give more than one file name in the command line.

You might wonder why you want to look at the beginning of a file. Let me give you an example: suppose you have several HTML files and you want to know if the first line (in each file) has the DOCTYPE information. Then you can look at the first line of each file with a command similar to this:

head -1 *.html

The command tail behaves in a similar way except that looks at the end of the file. So

tail file-name

will produce the last 10 lines of a file; called as

tail -15 file-name

will give the last 15 lines, while

tail -c 30 file-name

will give you the last 30 bytes of it.

There is one more useful option for tail; for example, if you do

tail +3 file-name

you will get all lines beginning at the 3rd line of the file.

An example of usage of tail is to check that all your HTML files end with . You can check the last line of each file with a command like this:

tail -1 *.html

No comments: