7+ useful head command’s switches with example – Unix/Linux

By -

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

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

Myself Arunlal, working as an administrator for both Linux and Windows servers. I am interested in Blogging, learning new things and technologies etc. I'm posting some interesting technical stuffs from my working experience. Let me know your suggestions as comment.

3 Comments to 7+ useful head command’s switches with example – Unix/Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *