Table of Contents
I think it’s safe to say that
head was one of the first Unix commands I learned.
Definitely up there with
Although, I find myself using its converse utility
tail much more often, digging into this reminded me how useful the
head command actually is.
Even if you’ve used this a million times before, I hope you can gain something out of this.
As with most Unix commands of their time, the name helps describe it all: display the top part of files.
I couldn’t find when the first release of a version of the
head command was created.
The copyright in the GNU source code states 1989.
I was wondering why they didn’t use the name top as its use is to display the top of the file.
But it seems that the
top command precedes GNU
head by about 5 years.
output the first part of files – head(1)
Did you know that there used to be a
head command on MSX-DOS?
Sadly, it didn’t find it’s way into MS-DOS, nor Windows.
The command prompt and powershell blew up in my face when I went to try using it.
head will output the first 10 lines of the document (less if that’s the case).
||quiet mode (GNU only). doesn’t display header|
||use bytes instead of lines|
||line delimiter is NUL, not newline (see last example)|
The default output using the command.
Let’s get some info on the
/etc/passwd file on my Macbook.
head /etc/passwd ## # User Database # # Note that this file is consulted directly only when the system is running # in single-user mode. At other times this information is provided by # Open Directory. # # See the opendirectoryd(8) man page for additional information about # Open Directory. ##
You can specify the amount of lines to output with the
n can be omitted in replace of an actual number and it will function the same.
head -n 6 /etc/group ## # Group Database # # Note that this file is consulted directly only when the system is running # in single-user mode. At other times this information is provided by # Open Directory.
# the same as head -n 6 /etc/group
In the GNU version you can specify a negative number to display all lines except the remaining
This isn’t available in the *BSD versions (soz macOS peeps).
I have a few extra hosts set up in
/etc/hosts that I don’t feel like displaying to the world.
I can use this technique to show the base file and exclude my extra eight hosts.
Since I know the last eight lines will always be mine.
Adding extra content anywhere will result in the same exclusion.
head -n -8 /etc/hosts ## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost # Added by Docker Desktop # To allow the same kube context to work on the host and the container: 127.0.0.1 kubernetes.docker.internal # End of section
Instead of the number of lines,
head can output
x number of bytes.
This can stop in the middle of a line.
In a pinch, you can use
head to help create a new password.
head -c 12 /dev/urandom | base64 dXNvc21hcnReX34K
Last one is a bit of a complicated example.
I couldn’t think of a practical example to use but I wanted to showcase the
If you wanted to use
head to limit data but it’s not using a newline char
\n, the zero-terminated option will look for the NUL terminator instead of
The output will be limiting the top two files that start with a in the
/etc directory. However, the output will be all on one line.
find /etc/* -iname "a*" -print0 | head -z -n 2 /etc/afpovertcp.cfg/etc/afpovertcp.cfg~orig
I hope you give
head a go next time you need to preview a file or limit some output.
Remember to always use the right tool for the job.