head and tail command in linux with examples. Head command in Linux allows you to prints the first ten lines of any file whereas Tail command ...
Head and Tail command in linux | with various Flags and Examples
![]() |
Head and Tail command in linux |
Hello guys Today I am going to show you how you can use "head" and "tail" command in Linux.
"head" and "tail" commands are generally used with files.
So the "head" command can be used to print the first part of the file.
The "tail" command can be used to print the last part of the file.
By "default" whenever you use a "head" command. It will print the first 10 lines and last 10 lines using "tail" command.
Let's see how we can use the "head" and "tail" command.
Head command
![]() |
head command |
$head fileName
For example, I have a file called "numbers". So I Just write "head numbers" and then press ENTER
It's going to show you the first 10 lines of this text file.
- This head command can be used in many ways. But generally, it's used with log files and other kinds of files.
Tail command
![]() |
Tail command |
$tail fileName
The "tail" command can be used to show the last 10 lines.
I just use "tail numbers" and then it's going to show the last 10 lines of this file.
Changing the default number of lines
![]() |
Change No. of Lines |
$head -n [No.ofLines] [fileName]
For example, I want to see just 3 lines are using the "head" command.
So I can just write "head -n3 numbers" and press ENTER
It's going to show the first 3 lines.
The same you can do with the "tail" command.
![]() |
Example |
$tail -n [No.ofLines] [fileName]
So you can just write "tail -n3 numbers" press ENTER It's going to show you the last 3 lines of the file.
- You can also use this "head" and "tail" command without "n" flag.
[-f] Follow flag
The other use of this "head" and "tail" command is using the flag "-f".
![]() |
-f flag |
$tail -f fileName
I'm going to just use "tail -f numbers" and press ENTER
It's printing the last 10 lines. But it's not going out of the terminal output.
So it's hanging around and it's going to watch for this file. Whenever this file changes then this output will also change.
So it's going to append the last line.
Press ctrl+c to Exit.
- "-f" flag is for follow.
With the "-f" flag you can also monitor whenever you are doing input and output operations or any other communication you are doing with your Linux operating system.
Access multiple files
One more thing you can do with the "head" and "tail" command.
You can use multiple files with this command.
![]() |
Access multiple files |
$head [file1] [file2]
Just write "head numbers letters" and press ENTER
You can see that first, it has printed the first 10 lines of the numbers file and then it has printed the 10 lines of this letters file.
Conclusion
So this is how you can use generally the "head" and "tail" commands in your Linux operating system.
I hope you've enjoyed this tutorial bye for now.
HAVE A NICE DAY💖