CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File Viewing & Editing / head

head

Show the first lines or bytes of a file

Beginneressentialviewingpipes

head prints the beginning of a file or a stream: 10 lines by default, or as many lines or bytes as you ask. It's how you peek at a CSV header or a big log's first entries, and how you keep only the top results of a sorted pipeline.

Syntaxhead [-n [-]NUM | -c [-]NUM] [-q | -v] [FILE]...

Watch it run

You want to know what a log or data file looks like before processing it.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
head /var/log/app/app.log

With no option, head prints the first 10 lines: enough to see the log format and when the file starts.

Anatomy of a command

$
commandps aux

List every process with its CPU and memory use.

How it works

head opens the file (or reads stdin) and reads it in blocks, counting newlines (with -n) or bytes (with -c) as it copies them to stdout, and exits as soon as it has printed enough. It never reads more than it needs, so head on a 50 GB file returns instantly. In a pipeline, head's exit closes the read end of the pipe; the next time the producer (ps, sort, yes, cat...) writes, the kernel sends it SIGPIPE and it terminates, which is why yes | head -3 finishes. A negative count, -n -5, means 'everything except the last 5 lines', which forces head to read to the end while holding back a buffer. With several files, head prints a ==> name <== header before each one unless -q is given.

Options you'll actually use

-n NFirst N lines (default 10); also written head -N
-n -NEverything except the last N lines
-c NFirst N bytes (accepts K, M: -c 1K)
-c -NEverything except the last N bytes
-qNever print ==> file <== headers
-vAlways print the header, even for one file

Common mistakes

head -100 app.log expecting the most recent lines

head shows the oldest lines of a log, the start of the file.

tail -n 100 app.log for the newest entries
head -c 100 notes.txt to get the first 100 characters

-c counts bytes, not characters: in UTF-8 text an accented letter or emoji takes 2-4 bytes and can be cut in half.

head -c is right for binary data; for characters use awk '{print substr($0,1,100)}' in a UTF-8 locale
Piping into head in a script with set -o pipefail and treating the failure as a bug

The producer may be killed by SIGPIPE (exit 141) when head exits early, which pipefail reports as a pipeline failure.

Expect it, or avoid pipefail for that line (e.g. { cmd || true; } | head)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show the first 3 lines of ~/data/orders.csv.

  2. 2

    Show the first line of every .txt file in ~/data, with file name headers.

  3. 3

    List the 5 largest files in ~/data/archive (recursively), biggest first.

  4. 4

    Generate a random 24-byte token, base64-encoded.

Related commands

tailShow the last lines of a file and follow it livecatPrint and concatenate filessortSort lines: alphabetically, numerically, by columnpsSnapshot the running processes
PrevioustacNexttail
Linux / File Viewing & Editing / head

head

Show the first lines or bytes of a file

Beginneressentialviewingpipes

head prints the beginning of a file or a stream: 10 lines by default, or as many lines or bytes as you ask. It's how you peek at a CSV header or a big log's first entries, and how you keep only the top results of a sorted pipeline.

Syntaxhead [-n [-]NUM | -c [-]NUM] [-q | -v] [FILE]...

Watch it run

You want to know what a log or data file looks like before processing it.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
head /var/log/app/app.log

With no option, head prints the first 10 lines: enough to see the log format and when the file starts.

Anatomy of a command

$
commandps aux

List every process with its CPU and memory use.

How it works

head opens the file (or reads stdin) and reads it in blocks, counting newlines (with -n) or bytes (with -c) as it copies them to stdout, and exits as soon as it has printed enough. It never reads more than it needs, so head on a 50 GB file returns instantly. In a pipeline, head's exit closes the read end of the pipe; the next time the producer (ps, sort, yes, cat...) writes, the kernel sends it SIGPIPE and it terminates, which is why yes | head -3 finishes. A negative count, -n -5, means 'everything except the last 5 lines', which forces head to read to the end while holding back a buffer. With several files, head prints a ==> name <== header before each one unless -q is given.

Options you'll actually use

-n NFirst N lines (default 10); also written head -N
-n -NEverything except the last N lines
-c NFirst N bytes (accepts K, M: -c 1K)
-c -NEverything except the last N bytes
-qNever print ==> file <== headers
-vAlways print the header, even for one file

Common mistakes

head -100 app.log expecting the most recent lines

head shows the oldest lines of a log, the start of the file.

tail -n 100 app.log for the newest entries
head -c 100 notes.txt to get the first 100 characters

-c counts bytes, not characters: in UTF-8 text an accented letter or emoji takes 2-4 bytes and can be cut in half.

head -c is right for binary data; for characters use awk '{print substr($0,1,100)}' in a UTF-8 locale
Piping into head in a script with set -o pipefail and treating the failure as a bug

The producer may be killed by SIGPIPE (exit 141) when head exits early, which pipefail reports as a pipeline failure.

Expect it, or avoid pipefail for that line (e.g. { cmd || true; } | head)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show the first 3 lines of ~/data/orders.csv.

  2. 2

    Show the first line of every .txt file in ~/data, with file name headers.

  3. 3

    List the 5 largest files in ~/data/archive (recursively), biggest first.

  4. 4

    Generate a random 24-byte token, base64-encoded.

Related commands

tailShow the last lines of a file and follow it livecatPrint and concatenate filessortSort lines: alphabetically, numerically, by columnpsSnapshot the running processes
PrevioustacNexttail