Friday, September 7, 2012

Use head and tail to read text files



There are many commands to view text files. The most used one is "cat" which is the most suitable to read small text files, "more" and "less" can be good options for large files. But if you are just interested in the beginning or the end of a large file, "head" and "tail" may be very handy for you.



As you can already guess from the name of the commands, "head" will show the beginning and "tail" will show the end of the file. By default, each command will show the first (or last) 10 lines of a file.  The most common use of "head" is:
 head text-file  

In case you want to read more or fewer lines, you can use the "-n" option followed with a certain number. For example, if you want to read the first 30 lines of a file, the command will be:
 head -n 30 text-file  

And like "head" that shows the first lines of a file, "tail" will show the last lines of a file. The use of "tail" is very similar to that of "head", for example:
 tail text-file  

And the "-n" option is used to change the number of the output lines:
 tail -n 7 text-file  

No comments:

Post a Comment