7 plus head command switches with example in Unix/Linux
The head command
By default, the head command prints the first 10 lines from the file which is given followed by the head command.
In the case of more than one file with head command displays separate output with a header identifying the file name. Head command has a lot of useful and also very helpful switches.
Useful switches with example:
“I’ve copied the ‘/etc/passwd’ and ‘/etc/shadow’ files to pass.txt and sha.txt respectively to demonstrate examples.”
1. head : The head command alone
Which displays the first 10 line by-default.
Example:
# head pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
........................
........................
2. head : More than one file
Displays separate output with a header identifying the file name.
Example:
# head pass.txt sha.txt
==> pass.txt <==
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin
........................
........................
==> sha.txt <==
root:$6$XbQGdyHM$BAYUxjJ46PNxBY.Yky8D8WrW9NAulFJT/Qd.9krMk/wL4oxK.OMFkaTF/2A8mQ47e4w.5URA5hG/1Cb7YHhwV0:16073:0:99999:7:::
bin:*:15240:0:99999:7:::
........................
........................
3. head -n : Displays last “n” lines from the text file
Example:
# head -n 3 pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
Introduction to IAAC [Infrastructure As A Code] tools
It’s time that I had to write an article on IAAC. All giant’s infra are now set and manage as IAAC.
I hope you guys are already aware of these tools and you are already started working on that. This is a simple, straight intro to those tools. This will help you to start your journey as an automation engineer.
4. -c, –bytes=K
Output the first K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file.
Example:
# head -c 100 pass.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
5. -q, –quiet, –silent
Never print headers giving file names.
6. -v, –verbose
Always print headers giving file names.
7. –help
Display this help and exit.
8. –version
Output version information and exit.
That’s it… 🙂
Related topics
The tail command
iftop command
find command switches with example
Top command
PS command usages with example
More:
groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls
4 thoughts on “Head command switches with example in Unix/Linux”