Heads or Tails?

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,526
Reaction score
3,292
Credits
31,543

Viewing the Beginning and Ending Lines of a File in Linux​

When working with large text files in Linux, it can be useful to quickly view the beginning or ending lines of the file. The head and tail commands are perfect for this task.

Using head to View the Beginning of a File​

The head command is used to display the first few lines of a file. By default, it shows the first 10 lines, but you can specify a different number of lines if needed.

Syntax:

head [OPTION]... [FILE]...

Example: To view the first 10 lines of a file named example.txt, you would use:
Code:
 head example.txt

To view the first 20 lines, you can use the -n option:
Code:
 head -n 20 example.txt

Using tail to View the Ending of a File​

The tail command is used to display the last few lines of a file. Like head, it shows the last 10 lines by default, but this can be adjusted.

Syntax:

tail [OPTION]... [FILE]...

Example: To view the last 10 lines of example.txt, you would use:
Code:
 tail example.txt

To view the last 20 lines, use the -n option:
Code:
 tail -n 20 example.txt

Combining head and tail to View the Middle of a File​

You can combine head and tail to view a specific range of lines from the middle of a file. For example, to view lines 11 to 20 of example.txt, you can first use head to get the first 20 lines and then tail to get the last 10 lines of that output.

Example:
Code:
 head -n 20 example.txt | tail -n 10

This command sequence works as follows:

  1. head -n 20 example.txt extracts the first 20 lines of example.txt.
  2. tail -n 10 then extracts the last 10 lines from the output of the head command, effectively giving you lines 11 to 20 of the original file.

I hope this helps you understand how to use head and tail in Linux!
 


If you want to watch, for instance, a log file as it is updated you can "follow" it using tail -f somelog.txt which will display the last ten lines and then wait, displaying new lines as they are added. If you want more than ten lines to start with, you can use something like tail -20 -f somelog.txt
 
If you want to watch, for instance, a log file as it is updated you can "follow" it using tail -f somelog.txt which will display the last ten lines and then wait, displaying new lines as they are added. If you want more than ten lines to start with, you can use something like tail -20 -f somelog.txt

I don't know why I didn't include that. Good catch.

Also, you can use uppercase -F if the file handle changes.

For example, we have an application that creates a new log file every hour.
The new file has the same name as the old log file, the old log file gets re-named ( the date and time get added to the file name )
So, the file name remained the same, the file handle changed.

tail -F application.log
 
It took me a while to figure out how to use /usr/bin/cut. I did finally figure it out. The man page just wasn't enough. Perhaps you could also write a tutorial about using /usr/bin/cut. Other Linux users may still be having trouble with that one. And maybe /usr/bin/sed while you're at it. Thanks in advance.

Signed,

Matthew Campbell
 
If you want to watch, for instance, a log file as it is updated you can "follow" it using tail -f somelog.txt which will display the last ten lines and then wait, displaying new lines as they are added. If you want more than ten lines to start with, you can use something like tail -20 -f somelog.txt

Thought the same ... the main reason to use TAIL is to follow "active files", so

tail -f filename.ext


Also in addition to getting the middle part of a file (like in the example, the 11th til the 20th line),
you can alo use it just to get any specific line (for example just the 20th line):

head -n 20 example.txt | tail -n 1

But I'm sure there's another command to do the same, but I can't get to think what that is.
Note that if the file has less than 20 lines, I think it just displays that last line.
 
Thought the same ... the main reason to use TAIL is to follow "active files", so

tail -f filename.ext


Also in addition to getting the middle part of a file (like in the example, the 11th til the 20th line),
you can alo use it just to get any specific line (for example just the 20th line):

head -n 20 example.txt | tail -n 1

But I'm sure there's another command to do the same, but I can't get to think what that is.
Note that if the file has less than 20 lines, I think it just displays that last line.
Using tail -n 1 should only produce one line of text output as per the man page. Using head -n 20 will provide the first 20 lines of text in your example file. Using tail -n 20 would produce the last 20 lines of text in your example file, or the last 20 lines of text received from stdin from the piped output from head. In each case it would be required for the example file to contain at least 20 lines of text in order for this to work as described.

If you wanted to provide the 20 middle lines from a text file that had at least 41 lines of text you might try using head -n 40 example.txt | tail -n 20. This would omit the first 20 lines of text from your example file along with the last line.

Perhaps you are thinking of using /usr/bin/cut to provide an alternate means of slicing up a text file. One could use a bash script to read a text file one line at a time and discard any unwanted portions. See SHELL BUILTIN COMMANDS -> read in the bash(1) man page for details on using read in bash.

Signed,

Matthew Campbell
 
Using tail -n 1 should only produce one line of text output as per the man page. Using head -n 20 will provide the first 20 lines of text in your example file. Using tail -n 20 would produce the last 20 lines of text in your example file, or the last 20 lines of text received from stdin from the piped output from head. In each case it would be required for the example file to contain at least 20 lines of text in order for this to work as described.

If you wanted to provide the 20 middle lines from a text file that had at least 41 lines of text you might try using head -n 40 example.txt | tail -n 20. This would omit the first 20 lines of text from your example file along with the last line.

Perhaps you are thinking of using /usr/bin/cut to provide an alternate means of slicing up a text file. One could use a bash script to read a text file one line at a time and discard any unwanted portions. See SHELL BUILTIN COMMANDS -> read in the bash(1) man page for details on using read in bash.

Signed,

Matthew Campbell

You're right, if you want 20 lines use 20 and if you want 1 line use 1
 


Members online


Top