head Command Examples in Linux

The head command does what you’d expect; it displays a file’s first group of lines (the file’s “head”). By default, it displays the first 10 lines of text:

$ head log_file
line1
line2
line3
line4
Hello World - line5
line6
line7
line8
line9
Hello again World - line10
$

Similar to the tail command, the head command supports the -n parameter so that you can alter what’s displayed. Both commands also allow you to simply type a dash along with the number of lines to display, as shown here:

$ head -3 log_file
line1
line2
line3
$

Usually the beginning of a file doesn’t change, so the head command doesn’t support the -f parameter feature as the tail command does. The head command is a handy way to just peek at the beginning of a file.

head Command Examples

1. To output first 10 lines from the file:

# head file.txt

2. To output first specified number of bytes from file:

# head -c 10 file.txt

3. To output first specified number of lines from file:

# head -n 15 file.txt

4. To never output headers giving file names:

# head -q file.txt
# head --quiet file.txt
# head --silent file.txt

5. To always print the headers giving file names:

# head -v file.txt
# head --verbose file.txt

6. To display the help for head:

# head --help

7. To print the version info for head:

# head --version
Related Post