i. Overview

In Linux, getting a portion of text from input files is a common operation.

In that location are two basic and widely used command-line utilities to output some parts of the text from the input: the head command and the tail control.

In this tutorial, we volition discuss the typical usages of these two commands through examples.

2. Introduction to the head and the tail Commands

Both the head and the tail commands are members of the GNU coreutils bundle. They are, past default, installed in all Linux distributions.

Equally their names imply, the caput command will output the get-go function of the file, while the tailcommand will impress the last part of the file. Both commands write the result to standard output.

In later on sections, we'll have a closer look at each control and learn how to utilize them through examples.

3. The caput Command

The syntax of the caput command is pretty straightforward:

          head [OPTIONS] FILES        

Permit's set a file (numbers_en.txt) as the input example to understand the control better:

          $ cat numbers_en.txt one           : 1 two           : 2 three         : three four          : iv ... xc-vii  : 97 ninety-eight  : 98 ninety-nine   : 99 ane hundred   : 100        

The file contains English words of numbers from ane to 100. Thus, the file has 100 lines.

The caput control will, by default, write the first ten lines of the input file to the standard output:

          $ head numbers_en.txt one           : 1 2           : two three         : 3 four          : 4 five          : 5 half-dozen           : vi seven         : 7 viii         : eight nine          : nine ten           : 10        

3.one. Output a Specific Number of Lines

With the-n selection, nosotros can allow thehead command output the first n lines instead of the default x.

For example, if nosotros desire to take the first vii lines printed to standard out, we'd use-n 7:

          $ head -n 7 numbers_en.txt i           : 1 two           : 2 3         : three four          : 4 five          : 5 six           : 6 7         : 7        

If we pass the -n choice together with a number following the , for example -n -x, the head control volition print all lines merely the last x lines of the file.

For instance, if we want to ignore the last 97 lines from the file, we'd do -n -97:

          $ head -n -97 numbers_en.txt one           : one two           : 2 three         : iii        

3.ii. Output a Specific Number of Bytes

In add-on to displaying text by line, the head command can also print the file content past byte if we laissez passer the -c option.

The usage of the-c option is the aforementioned equally the -n selection except for displaying text byte-wise instead of line-wise.

Permit'south see an case of displaying but the kickoff word "one" (3 bytes) from the file:

          $ caput -c 3 numbers_en.txt i        

3.three. Output Multiple Files

The caput control can besides handle multiple files. To encounter this, let's first prepare another input file numbers_de.txt. This file is very similar to the numbers_en.txt. The merely difference is that the words in the file are in High german instead of English:

          $ cat numbers_de.txt eins             : 1 zwei             : 2 drei             : iii vier             : 4 ... siebenundneunzig : 97 achtundneunzig   : 98 neunundneunzig   : 99 (ein)hundert     : 100        

Now let's output the commencement five lines from both files in one shot:

          $ head -north v numbers_en.txt  numbers_de.txt ==> numbers_en.txt <== one           : 1 two           : two 3         : three four          : 4 five          : 5  ==> numbers_de.txt <== eins             : 1 zwei             : two drei             : three vier             : 4 fünf             : 5        

4. The tail Command

The syntax of using thetail command is quite straightforward, as well:

          tail [OPTIONS] FILES        

The tail control will past default write the last ten lines of the input file to the standard output:

          $ tail numbers_en.txt  xc-i    : 91 ninety-ii    : 92 ninety-three  : 93 ninety-iv   : 94 xc-five   : 95 ninety-vi    : 96 ninety-vii  : 97 ninety-eight  : 98 ninety-ix   : 99 one hundred   : 100        

four.ane. Output a Specific Number of Lines

With the-n option, we can let thetail command output the final northward lines instead of the default 10.

This instance shows how to get the last seven lines from the input file:

          $ tail -n 7 numbers_en.txt ninety-four   : 94 xc-5   : 95 ninety-six    : 96 ninety-seven  : 97 xc-eight  : 98 90-nine   : 99 one hundred   : 100        

If we pass the -n pick together with a number following the "+", for example "-n +x", the tail command will impress starting with the x-th line till the cease of the file.

Permit's print from 95th line till the end of the numbers_en.txt file:

          $ tail -north +95 numbers_en.txt xc-five   : 95 90-six    : 96 ninety-7  : 97 ninety-eight  : 98 ninety-nine   : 99 ane hundred   : 100        

iv.2. Output a Specific Number of Bytes

Similar to thehead command, if we laissez passer-c x option to thetail control, it volition output only the concluding x bytes from the input file.

Let's become the last number 100 from the input file:

          $ tail -c 4 numbers_en.txt 100        

In the example higher up, nosotros passed iv instead of 3 to the-c option. This is because there is an ending linebreak in the final line, and this linebreak occupied 1 byte.

4.3. Output Multiple Files

Like the head control, thetail command can output from multiple input files, too.

Let's have a wait at an example of printing the last five lines from two input files:

          $ tail -n v numbers_en.txt numbers_de.txt  ==> numbers_en.txt <== ninety-six    : 96 ninety-seven  : 97 ninety-8  : 98 ninety-ix   : 99 one hundred   : 100   ==> numbers_de.txt <== sechsundneunzig  : 96 siebenundneunzig : 97 achtundneunzig   : 98 neunundneunzig   : 99 (ein)hundert     : 100        

4.4. Lookout a File for Changes

Sometimes the input file we want to check is changing. For instance, a running awarding may append its output to a log file.

If we execute the tail command with the-f option on the changing file, all newly added lines volition be appended to standard out.

Next, let's run across a sit-in of this characteristic.

First, we'll create a simple vanquish script append_ts.sh. Every second information technology appends the current timestamp to a log file (/tmp/timestamp.log), information technology will append to the log file x times:

          $ cat ./append_ts.sh #!/bin/bash for times in {ane..ten} do     echo $(date) >> /tmp/timestamp.log     times=$(($times+1))     slumber one done        

Now, let's start watching the log file past thetail command, and then we execute the script to a higher place and observe if the newly suspend timestamps come totail'south output:

As we expected, all newly appended lines are printed out.

To exit the tail command, we tin can printing Ctrl-C.

v. Apply the caput and the tail Together

We've learned that thehead command can give u.s.a. the showtime part of a file, while thetail command tin output the final role of the input file. But what if we want to get some office in the middle of a file?

To solve this problem, we can combine the two simple commands.

Permit's say we desire to get from the 51st to the 55th line from an input file:

          $ tail -n +51 numbers_en.txt | head -northward 5 fifty-one     : 51 50-two     : 52 fifty-iii   : 53 50-four    : 54 l-5    : 55        

We can besides become the same output by combining the head command and the tail command in a different way:

          $ head -north 55 numbers_en.txt | tail -due north 5        

6. Conclusion

The head and the tail are 2 simple and straightforward Linux command-line utilities.

In this commodity, we've learned some typical usages of both commands through examples.

If we use the 2 commands properly, nosotros tin easily go the parts of input files for farther processing.

Authors Bottom

If you accept a few years of experience in the Linux ecosystem, and you're interested in sharing that feel with the customs, accept a look at our Contribution Guidelines.