head and tail command in linux with examples | Pro Tech guides

head and tail command in linux with examples

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 with 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 in linux with examples
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 in linux with examples
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

head and tail command in linux with examples
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 numbersand press ENTER

It's going to show the first 3 lines.

The same you can do with the "tail" command.

head and tail command in linux with examples
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".

Linux command head and tail with -f flag with examples
-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.

head and tail command in linux with examples
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💖

Name

Apps,3,Games,1,Linux,38,Linux Commands,37,Network-Devices,1,Networking,1,Terminal Apps,2,Termux,17,Userland,3,Windows,1,
ltr
item
Pro Tech guides: head and tail command in linux with examples
head and tail command in linux with examples
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 ...
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_8yga6sez7HRllQqrwtDFLmLNIgACxeg9cMEOhpUcNck82iiW0EX7ETF9EpKEVdOXrTlOCdNY7GX_2tZV2_NMY4Oa90F0T1pfv4ax1GCdhVNubou2ly06tXMok6zqCwXBLhSqpL8-_Ic/s16000/headandtail.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_8yga6sez7HRllQqrwtDFLmLNIgACxeg9cMEOhpUcNck82iiW0EX7ETF9EpKEVdOXrTlOCdNY7GX_2tZV2_NMY4Oa90F0T1pfv4ax1GCdhVNubou2ly06tXMok6zqCwXBLhSqpL8-_Ic/s72-c/headandtail.jpg
Pro Tech guides
https://devops-digest.blogspot.com/2021/01/head-and-tail-command-in-linux.html
https://devops-digest.blogspot.com/
https://devops-digest.blogspot.com/
https://devops-digest.blogspot.com/2021/01/head-and-tail-command-in-linux.html
true
7053638852026728047
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content