Head and Tail Commands in Linux with Examples

According to Wikipedia, Linux is a family of open-source operating system based on Linux Kernel. This family includes almost all open source operating systems like Ubuntu, Fedora, Debian and Linux Mint. And, it has a very powerful set of commands that we can use to perform almost all kind of operations on operating system. Two of those commands are Head and Tail. In this article, I will show you how to use Head and Tail commands in Linux with examples.

So, let’s first understand what head and tail commands are and what they are used for. The simplest definition of Head would be to display the first X number of lines in the file. And the Tail displays the last X number of lines in the file.

By default, the head and tail commands will display the first or last 10 lines from the file. But there are many flags that we can define while executing the command to get the customised output. So, let’s get started with the actual tutorial.

Head and Tail commands in Linux with Example

Let’s get started with the head command. Both the commands are very easy to understand and are very simple in use.

HEAD COMMAND IN LINUX

The syntax for the Head command is as follows.

head {OPTIONS} {FILE}

In this syntax, The options are optional, It means, you can directly execute head {FILE} to get the first 10 lines of any file as an output. Just like this.

head /etc/ssh/sshd_config

Here in this command, we are only using head {FILE}syntax to display the first 10 lines of the SSH configuration file. But, if you want to get specific number of lines from top of the file, use the command with -n, --lines flags. Let’s say we want to get the first 5 lines from the same configuration file, then the command would be.

head -n5 /etc/ssh/sshd_config # OR head –lines=5 /etc/ssh/sshd_config

Similarly, if we want to get the first X bytes from the file, the flags would be -c, --bytes. For example, if we want to get the first 15 bytes from the file, we can execute the following command.

head -c15 /etc/ssh/sshd_config # OR head –bytes=15 /etc/ssh/sshd_config

If you are interested in seeing the file headers, execute the command with -v flag. Just like the following example.

head -v /etc/ssh/sshd_config

So, this is how you can use Head command in Linux based operating systems. Now, let’s take a look at the tail command.

TAIL COMMAND IN LINUX

Tail command in Linux is same as the head command. However, it displays the last X number of lines/bytes from the file. Here is the syntax for tail command in Linux.

tail {OPTIONS} {FILE}

Again, the options are optional. By default, the tail command displays the last 10 number of lines from the file. So, if you want to see the last 10 logs in authentication log file, execute the following command.

tail /var/log/auth.log

If you want to display the specific number of lines from the end of the file, execute the command with -n, --lines flags. In this example, we will fetch last 15 logs from the authentication log file.

tail -n15 /var/log/auth.log

Similarly, if you want to see the last X bytes from the file, execute the command with -c, --bytes flags. Just like the following command that will fetch last 12 bytes from the file.

tail -c12 /var/log/auth.log

There is one more interesting feature in Tail command. You can follow the file with tail command, which means, the data will be appended and displayed in output in real time, really good while migrating the applications. Use -f, --follow flags to follow the file just like the following example.

tail -f /var/log/auth.log

Once executed, you will be able to see new logs written in the file instantly. Tail is a very handy command in Linux with very simple but meaningful features.

So, this is how you can run head and tail commands in Linux. I hope you enjoyed this article for learning head and tail command in Linux with examples. If you are interested in text processing in Linux, you would love to use Grep command in Linux. Let us know if you are facing any issue understanding the topic.

Was this answer helpful? 0 Users Found This Useful (0 Votes)