head Study Notes

head

prints the first part of a file (default is first 10 lines)

head file.txt

Multiple files

head file1.txt file2.txt

Output:

==> file1.txt <==
(first 10 lines)

==> file2.txt <==
(first 10 lines)

it separates files with ==> headers <==


-q, –quiet, –silent`

Never print file name headers.


-v, --verbose

Always print file name headers, even for one file:

head -v file.txt

-c

head -c 20 file.txt

shows first 20 bytes (not lines)

If file contains:

Hello World
Linux

Then:

head -c 5 file.txt

Output:

Hello

-n -5 file.txt

Means:

“show everything EXCEPT last 5 lines”

Example:

File:

1
2
3
4
5
6
7
8

Command:

head -n -3 file.txt

Output:

1
2
3
4
5

So:

remove last N lines instead of taking first N


Same works for bytes:

head -c -10 file.txt

→ show everything except last 10 bytes


Size suffixes

You can write sizes like (default bytes):

‘b’  =>            512 ("blocks")
‘KB’ =>           1000 (KiloBytes)
‘K’  =>           1024 (KibiBytes)
‘MB’ =>      1000*1000 (MegaBytes)
‘M’  =>      1024*1024 (MebiBytes)
‘GB’ => 1000*1000*1000 (GigaBytes)
‘G’  => 1024*1024*1024 (GibiBytes)

and so on for ‘T’, ‘P’, ‘E’, ‘Z’, ‘Y’, ‘R’, and ‘Q’. Binary prefixes can be used, too: ‘KiB’=‘K’, ‘MiB’=‘M’, and so on.