# head — Show the First Lines of Files

> Practical guide to head: show the first lines or bytes of a file, check headers and limit output in pipelines.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/head/

<!-- PROSE:intro -->
head shows you the beginning of a file – by default the first 10 lines. With it you take a quick look inside large files, check the header row of a CSV or limit output in pipelines to the first hits. Use `-n` to set the line count and `-c` to work byte by byte. Handy: GNU head also understands negative counts such as `-n -3` (everything but the last three lines). This guide walks you through the common options and their typical pipeline partners.
<!-- PROSE:intro:end -->

## Basic Usage

`head <file>` — Show the first 10 lines of a file (default).

```bash
head access.log
```

`head -n <count> <file>` — Show the first N lines of a file.

```bash
head -n 20 access.log
```

`head -<count> <file>` — Shorthand for -n. Show the first N lines.

```bash
head -5 data.csv
```

`head -n -<count> <file>` — Show all lines except the last N lines.

```bash
head -n -3 data.txt
```

## Byte Mode

`head -c <bytes> <file>` — Show the first N bytes of a file instead of lines.

```bash
head -c 100 binary.dat
```

`head -c <size>K <file>` — Show the first N kilobytes. Supports K, M, G suffixes.

```bash
head -c 4K large-file.bin
```

`head -c -<bytes> <file>` — Show all bytes except the last N bytes.

```bash
head -c -10 data.bin
```

## Multiple Files

`head <file1> <file2>` — Show the first 10 lines of each file with filename headers.

```bash
head error.log access.log
```

`head -n <count> <file1> <file2>` — Show the first N lines of multiple files.

```bash
head -n 5 *.log
```

`head -q <file1> <file2>` — Quiet mode. Suppress filename headers when showing multiple files.

```bash
head -q -n 1 *.csv
```

`head -v <file>` — Verbose mode. Always show filename header, even for a single file.

```bash
head -v -n 5 data.txt
```

## Pipelines

`<command> | head` — Show only the first 10 lines of command output.

```bash
ps aux | head
```

`<command> | head -n <count>` — Limit command output to the first N lines.

```bash
find / -name '*.conf' 2>/dev/null | head -n 20
```

`<command> | head -n 1` — Get only the first line of output (e.g. header row or first result).

```bash
docker ps | head -n 1
```

`sort <file> | head -n <count>` — Show the first N items after sorting (e.g. smallest values).

```bash
sort -n scores.txt | head -n 5
```

`<command> | head -c <bytes>` — Limit output to a specific byte count from a pipeline.

```bash
cat /dev/urandom | head -c 32 | base64
```

## Common Patterns

`head -n 1 <file>` — Show only the header line of a CSV or TSV file.

```bash
head -n 1 data.csv
```

`head -n 1 <file> && tail -n +2 <file> | sort` — Print header line, then sort the remaining data lines.

```bash
head -n 1 data.csv && tail -n +2 data.csv | sort -t',' -k2
```

`head -c 4 <file> | xxd` — Check the magic bytes (file signature) of a binary file.

```bash
head -c 4 image.png | xxd
```

`for f in *.log; do echo "=== $f ==="; head -n 3 "$f"; done` — Preview the first 3 lines of each log file with custom headers.

```bash
for f in *.log; do echo "=== $f ==="; head -n 3 "$f"; done
```

<!-- PROSE:outro -->
## Conclusion

head is the natural counterpart to `tail` and one of the fastest ways to look at a file without loading it whole – ideal for huge files too, since head simply stops reading once it has enough lines. Note that the negative counts (`head -n -3`, `head -c -10`) are a GNU extension and do not work on BSD or macOS. To cut out a middle range of lines, combine head with `tail`, for example `head -n 20 | tail -n 5`. With `-c` head reads binary data too – but take care mixing `-n` and `-c`, as the two are mutually exclusive.

## Further Reading

- [GNU Coreutils manual: head](https://www.gnu.org/software/coreutils/manual/html_node/head-invocation.html) – complete reference for every option
- [man7.org: head(1)](https://man7.org/linux/man-pages/man1/head.1.html) – the Linux manual page
<!-- PROSE:outro:end -->

## Related Commands

- [tail](https://www.jpkc.com/db/en/cheatsheets/files-text/tail/) – show the end of files and follow logs
- [less](https://www.jpkc.com/db/en/cheatsheets/files-text/less/) – page through files screen by screen
- [sed](https://www.jpkc.com/db/en/cheatsheets/files-text/sed/) – edit and filter text streams line by line

