Linux head Command Options with Examples – Display First Part of Files

In Linux system, we have some alternatives about how to display a content of text files. We can use cat command, more command or less command. Another command to do it is head command.

What is head command

Head is command that will print only the first part of a text file. So when you know you need only the first part of the file, head will help you. By default, it will display the first 10 lines.

How to use head command

To use head command is quite easy. We just need to type :

$ head file_name

For example, we have a file with 15 lines inside it.

Original file

When we run head command, then the result will be like the screenshot below.

Head command

This output will be the same if we use -q option.

Display based on line

We already know that head by default will print the first 10 lines. But we can override this setting using -noption followed by the number of line. For example, if we want to display the first 5 lines, then the command will be like this :

$ head -n 5 doc_10.txt

or

$ head -5 doc_10.txt

Display based on lines

Display based on file size

We can also view the content based on file size. For example, the doc_10.txt file has a size of 447 bytes. If we want to know the content of the first 100 bytes, we can use -c option followed by the size. Here’s an example.

Display by size

Please notice that the result may not look neat, since head will cut the result based on size.
-n option may have a multiplier suffix :

  • b =512
  • kB = 1000
  • K = 1024
  • MB = 1000 * 1000
  • M = 1024 * 1024
  • GB = 1000 * 1000 * 1000
  • G = 1024 * 1024 * 1024

Display the file name as header

You may need this information if you copy the result into another file. Here’s a sample of the header of a file named doc_10.txt

$ head -v doc_10.txt

Print the file name

Display multiple files

To display multiple files, you can put the files separated by space. Here’s a sample.

$ head -7 doc_10.txt doc_30.txt

View multiple files

Conclusion

This command is also one of the basic command in Linux system. Once again, if you are only need the first part of the file, head may the right tool for you. As usual, you can alway type man head or head –help to display its manual page to explore more detail.

5 UNIX / Linux head Command Examples

What is head?

Head prints the first N number of data of the given input. By default, it prints first 10 lines of each given file.

5 head Examples

1. Print the first N number of lines

To view the first N number of lines, pass the file name as an argument with -n option as shown below.

$ head -n 5 flavours.txt 
Ubuntu
Debian
Redhat
Gentoo
Fedora core

Note: When you simply pass the file name as an argument to head, it prints out the first 10 lines of the file.

2. Print N number of lines by specifying N with –

You don’t even need to pass the -n option as an argument, simply specify the N number of lines followed by ‘-‘ as shown below.

$ head -4 flavours.txt
Ubuntu
Debian
Redhat
Gentoo

3. Print all but not the last N lines

By placing ‘-‘ in front of the number with -n option, it prints all the lines of each file but not the last N lines as shown below,

$ head -n -5 flavours.txt 
Ubuntu

4. Print the N number of bytes

You can use the -c option to print the N number of bytes from the initial part of file.

$ head -c 5 flavours.txt
Ubuntu

Note : As like -n option, here also you can pass ‘-‘ in front of number to print all bytes but not the last N bytes.

5. Passing Output of Other command to Head Input

You may pass the output of other commands to the head command via pipe as shown below,

$ ls | head
bin
boot
cdrom
dev
etc
home
initrd.img
lib
lost+found
media

Syntax and Options

head [OPTIONS]… [FILE]…

Short Option Long Option Option Description
-c –bytes to print N bytes from each input file.
-n –lines to print N lines from each input file.
-q –silent, –quiet Prevent printing of header information that contains file name
-v –verbose to print header information always.

7+ useful head command’s switches with example – 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

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.

Advertisements

Posted on June 16, 2015, in Linux (Ubuntu/CentOS). Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: