head tail command linux

Head and Tail Commands in Linux

The essence of all console utilities in GNU/Linux comes down to one simple rule: do one thing, but do it well. And commands like head and tail are no exception. With their help, the productivity of the terminal accelerates significantly. This article covers these two utilities.

Their main task is to output 10 lines from the “head” or from the “tail” of a document or data from another program (in other words, incoming data). This number of lines is set by default, but thanks to the options, the output capabilities can be significantly improved.

Syntax and Options Head/tail

These commands are pretty easy to use. Their main difference is that “head” works from the beginning of incoming data, and “tail”—from the end.

Here is their syntax when working with a file:

head [options] file

tail [options] file

If the utility processes the data of another program, then everything works according to the standard rules of the shell:

previous command | head

previous command | tail

The options is an optional parameter: without it, the 10 lines of incoming data will be displayed. Here are frequently used options:

Short (BSD) form Long (GNU) form Value
-c –bytes=[-]N sets the number of bytes to output instead of whole lines; when using the minus sign — displays all content except the last (for head) or first (for tail) N bytes
-n –lines=[±]N sets the number of bytes to output instead of whole lines; when using the minus sign — displays all content except the last (for head) or first (for tail with plus sign) N bytes
-q –quiet, –silent shows the contents of a file without its name; works by default
-v –verbose Shows the file name first

Specifying the number of bytes output using the –bytes option, you can use the multiplier suffix: b (512), kB (1000), K (1024), MB (1000 × 1000), M (1024 × 1024), etc. Instead of single-letter pointers, KiB, MiB, etc. are also applicable.

There are other options for tail that complement its functionality. They are quite specific. You can read more about them in the tail help documentation. But here it is worth mentioning the -f option, which displays the contents of the file in real time (especially useful when tracking logs).

Examples of using Head/Tail Command

There are lot of options for using head and tail. In fact, only your imagination can be a limitation.

Output the First/last 10 Lines of a File

The easiest way to use utilities is to specify them without parameters:

head /var/log/pacman.log

head command in linux

tail /var/log/pacman.log

linux head tail

You can also output multiple files in a row. In this case, the file names are specified by default:

head file math.txt

tail command in linux

Output a Specific Range of Lines

By combining head and tail, you can display the specified range of lines/bytes of text. Here, for example, the output from line 25 to line 40 of the pacman.log:

cat -n /var/log/pacman.log | head -n 40 | tail -n +25

tail linux

Track File Changes

A very useful option, which in its functionality resembles the watch utility. It allows you to display the contents of the file online.

The external difference from the usual output is the absence of the prompt line, because the process is in an active state.

tail -f /var/log/pacman.log

tail command

To exit this mode, press Ctrl + C.

Add Your Comment