Often we only need to see the beginning or end of a file to find what we’re looking for. The
tail commands offer exactly this functionality. Here’s some more info on these commands from Easy Linux Commands.
Displaying Beginning Lines of a File
Sometimes a user might have a large file for which they only need to display the first few lines. For instance, perhaps the user would like to see the error code on a dump file and the code and error messages appear within the first fifteen lines of the dump file. The following example demonstrates how to display the first fifteen lines of a file using the head command. The head command takes a number as an option and uses it as the number of lines to be displayed. The default is 10.
$ head -15 declaration.txt
The Declaration of Independence of the Thirteen Colonies
In CONGRESS, July 4, 1776
The unanimous Declaration of the thirteen united States of America,
When in the Course of human events, it becomes necessary for one people to dissolve the political
bands which have connected them with another, and to assume among the powers of the earth, the
separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent
respect to the opinions of mankind requires that they should declare the causes which impel them
to the separation.
We hold these truths to be self-evident, that all men are created equal, that they are endowed by
their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit
of Happiness. --That to secure these rights, Governments are instituted among Men, deriving their
just powers from the consent of the governed, --That whenever any Form of Government becomes
In this example and often in use it may seem like head is displaying more lines than you asked for. That typically is because the lines are too long for the display so a single line may be continued on the next line.
Displaying Ending Lines of a File
The need might arise to see only the last lines of a file. A good example of this might be an error log file where the user would like to see the last few messages written to the log. The tail command can be used to display the last lines of a file, while passing the number of lines to be displayed. The following example requests the last eight lines in the file called declaration.txt.
$ tail -8 declaration.txt
they are accustomed. But when a long train of abuses and usurpations, pursuing invariably the
same Object evinces a design to reduce them under absolute Despotism, it is their right, it is
their duty, to throw off such Government, and to provide new Guards for their future security.
â€”Such has been the patient sufferance of these Colonies; and such is now the necessity which
constrains them to alter their former Systems of Government. The history of the present King of
Great Britain [George III] is a history of repeated injuries and usurpations, all having in
direct object the establishment of an absolute Tyranny over these States. To prove this, let
Facts be submitted to a candid world.
Again it appears we are getting more than eight lines, but this is just the result of long lines wrapping onto two lines.
Display Active Writes to a File
Sometimes you need to go one step further and watch as lines are being written to a file. Perhaps, for example, an application is compressing and copying files to an alternate location, writing messages to a log file called message.log as it processes each file. A curious user might want to observe the progress of the application. In this case, the tail command with the â€“f (follow) option can be used to read the messages as they are written to a file. The following example assumes that the current working directory is the same directory where the log file resides.
$ tail -f message.log
ï€¥A clever Linux user can also use the less command to display the beginning lines of a file, the ending lines of a file, or to follow active writes to a file like tail â€“f does. See the man entry for the less command to see how this is done.
For more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.
Buy it now!