- [Narrator] Welcome to the next video of section three,
Using Head and Tail for Printing
the Last or First Ten Lines.
In the previous video we've learned how to find
the difference between files and patching.
In this video, we'll see how to use
head and tail for printing the last or first ten lines.
When looking into a large file
which consists of thousands of lines,
we'll not use a command such as CAT
to print the entire file contents.
Instead we look for a sample.
For example, the first ten lines of the file
or the last ten lines of the file.
We may need to print the first end lines
or last end lines, and even print all the lines
except the last end lines, or all lines
except the first end lines.
Another use case is to print lines from nth to nth lines.
The commands head and tail can help us do this.
The head command always reads the header
portion of the input file.
Print the first ten lines as follows.
Read the data from standard n as follows.
Specify the number of lines to be printed as follows.
This command prints four lines.
Print all lines excluding the last m lines as follows.
Note that this is negative m.
For example, to print all the lines
except the last five lines,
use the following command.
The following command will, however,
print from one to five.
Printing by excluding the last lines
is very important usage of head.
Now let's see how to print last few lines.
Print the last few lines of a file as follows.
In order to read from standard n,
you can use the following command line.
Print the last five lines as follows.
In order to print all lines excluding
the first m lines, use the following code.
For example, to print all lines except the first five lines,
M plus one equals six,
therefore the command will be as follows.
This will print from six to 100.
One of the important usages of tail
is to read a constantly growing file.
Since new lines are constantly appended
to the end of the file,
tail can be used to display all new lines
as they are read into the file.
When we run tail without specifying any options,
it will read the last 10 lines and exit.
However, by that time new lines
will have been appended to the file by a process.
To constantly monitor the growth of file,
tail has a special option.
Minus F or dash dash follow,
which enables tail to follow the appended lines
and keep being updated as the data is added.
You'll probably want to use this on log files.
The command to monitor the growth
of the log files would be ...
We frequently run dmesg to look
up kernel ring buffer messages
either to debug the USB devices or to look at SDX.
X is the minor number for the SD device.
corresponding to a scuzzy disk.
The minus f tail can also add a sleep interval, minus s,
so that we can set the interval during which
the file updates are monitored.
Tail has the interesting property that allows
it to terminate after a given process ID dies.
Suppose we are reading a growing file
and process foo is appending data to the file.
The f tail should be executed until the foo process dies.
When the process foo terminates, tail also terminates.
Create a new file.txt and open the file in gedit.
You can any text editor.
Add new lines to the file
and make frequent file saves in gedit.
Now run the following commands.
When you make frequent changes to the file,
it will be written to the terminal by the tail command.
When you close gedit, the tail command will get terminated.
Awesome, we've successfully learned about using
head and tail for printing the last or first ten lines.
In the next video, we'll see
listing only directories - alternative methods.