headShow the first lines or bytes of a file
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.
head [-n [-]NUM | -c [-]NUM] [-q | -v] [FILE]...You want to know what a log or data file looks like before processing it.
ps auxList every process with its CPU and memory use.
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.
-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 filehead -100 app.log expecting the most recent lineshead shows the oldest lines of a log, the start of the file.
tail -n 100 app.log for the newest entrieshead -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 localePiping into head in a script with set -o pipefail and treating the failure as a bugThe 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)Try these on a Linux machine of your own. Each task has a hint and a solution.
Show the first 3 lines of ~/data/orders.csv.
Show the first line of every .txt file in ~/data, with file name headers.
List the 5 largest files in ~/data/archive (recursively), biggest first.
Generate a random 24-byte token, base64-encoded.
headShow the first lines or bytes of a file
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.
head [-n [-]NUM | -c [-]NUM] [-q | -v] [FILE]...You want to know what a log or data file looks like before processing it.
ps auxList every process with its CPU and memory use.
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.
-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 filehead -100 app.log expecting the most recent lineshead shows the oldest lines of a log, the start of the file.
tail -n 100 app.log for the newest entrieshead -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 localePiping into head in a script with set -o pipefail and treating the failure as a bugThe 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)Try these on a Linux machine of your own. Each task has a hint and a solution.
Show the first 3 lines of ~/data/orders.csv.
Show the first line of every .txt file in ~/data, with file name headers.
List the 5 largest files in ~/data/archive (recursively), biggest first.
Generate a random 24-byte token, base64-encoded.