Tail is a core application of Unix-based systems, designed to read the end of text files. It can also be used for something else called piped-data too, but we’ll touch on that later (let’s not complicate things just yet!).
Although “reading the end of a file” sounds pretty self-explanatory, tail is a chief tool in any expert’s arsenal, with many practical uses we’ll explore along the way.
To get started, open a terminal in a folder where you have a text file that will serve as a good experimental example, the examples shown here will be a text file containing the lyrics to a personal favourite song. Once you’ve chosen a good file, you can get started by entering the tail command, followed by the name of the file you have chosen, like this:
By default, tail displays a file’s last ten lines, but this can be altered by use of what’s known as a ‘switch’. To introduce you to what may be your first switch, let’s change that default output from 10 lines to 15 lines of text output.
For general terminal syntax, a switch usually consists of either one or two
hyphens followed by some symbolic characters, that sits in between
the command and the file name. If you’re having trouble digesting that, it’s simpler than it sounds. For our 15-line example, the switch used is -n followed by the number you want. So in practice, to change our last command to 15 lines of output simply looks like this:
tail -n 15 TheTrollSongLyrics.txt
But what if you only want to read the last line of a file? To
read one line looks like this:
tail -n 1 TheTrollSongLyrics.txt
And in case you were wondering, yes, there’s an opposite
command for reading the start of a file, and yes, it is called
head. To look at the first 15 lines of a file, you would type the
head -n 15 TheTrollSongLyrics.txt
And for those that just want to read the entire file for now,
try the less command:
Hopefully this was useful for some new Linux users how it works and veterans what they may have missed.