In the past, I have shared examples of essential commands like the grep command, find command, curl, netstat, nslookup, lsof and more and In this article, You will be learning how to use the head and tail commands in Linux. What is the head command? think of it as something at the top or above. Even as the name implies. same thing as the tail too, something below.
In Linux, when you use a head command, basically you are calling some set of data or information that is at the top. you would often work with a text file and manipulate it. You could specify the numbers of the line you want to see in the text file. let's say for instance we have a text file with a list of information inside.
How to use head and tail command in Linux
1. To Bring out the top information
$ head sample.txt
So, in the text file "sample.txt" it is going to bring out the first 10 items. Here is an example where you can see the first 10 lines of a json file which contains more than 2833 lines
2. To Bring out the list with the file name
$ head -v sample.txt
This command written above is somewhat similar to the first one. The only difference is that we the option -v is used with it. Using this command you would still have the top 10 but the name of the file would be seen above first, then the top 10 information
3. To print out a specified number of lines of information
$ head -n 6 sample.txt
This simply means that there will be a list of the first six pieces of information in the sample.txt file. The difference between this and the first command provided is that the particular command brings the first 6 while the first one brings the first 10. you could specify different types of numbers based on what you need.
4. To print a specific number of bytes
$ head -c 3 sample.txt
This is similar to the previous command before this. the difference is in the option. this time around it is an option -c that enables us to do that.
5. To output multiple files
We have been working with a demo file before now named sample.txt. Let us say for instance another file has been loaded namely "sample2.txt". So we want to output these 2 files, see the command below:
$ head -n 5 sample.txt sample2.txt
The command will result in displaying two different files. you can always output as many files as you want, you only need to separate them by spaces. Don't forget this command will give us the first 5 lines of each file respectively.
Now at this point. The tail Command shall be explained. the head command was explained initially. Tail is the opposite of head if you call the tail command on a text file, this will bring out the last 10 lines. let's see
6. To display lines of information using tail
$ tail sample.txt
this brings the last 10 lines, remember the default is 10
7. To bring out the specified number of lines
$ tail -n 5 sample.txt
The command above would bring out the last 5 lines
8. To output multiple files.
Given that we have two files already, let's take for instance demo1 and demo2 respectively. If you want to output both files, then you would do the following
$ tail demo1.txt demo2.txt
In this case, it is going to output the last 10 lines of both files one after the other. you could specify more if that is what you need.
9. To print a file with its name.
$ tail -v sample.txt
This command will result in having the last 10 lines with the name of that file
10. Using the tail and head together.
There could be instances you may want to use both the head and tail together. let's say you need the first 5 information and the last 5 information. here is the command for it
$ head -n 5 sample.txt | tail -n 5
- 10 Linux command line courses for Beginners (courses)
- 5 Example of kill commands in Unix and Linux (example)
- 10 examples of Networking commands in Unix (nslookup)
- How to set up cron jobs in Linux (Crontab example)
- VI Editor examples and tips for beginners (vi examples)
- 10 examples of lsof command in Linux? (examples)
- How does the nslookup command work in UNIX? (answer)
- How to use the netstat command to find which process is listening on a port? (example)
- 7 Best Linux Courses for DevOps Engineers (Linux courses)
- Linux find + du + grep example (example)
- 10 Examples of curl command in Linux (cURL)
- 10 Examples of chmod command in Linux (chmod)
Thanks for reading this article so far. If you like this article and my explanation of sending an email from a Linux machine then please share it with your friends and colleagues.
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.