The head command in Linux displays the first part of a file or input stream, typically the first 10 lines by default. It's built into most Unix-like systems and requires no extra installation.

What You'll Need

  • A Linux system or other Unix-like environment (e.g., macOS, WSL on Windows)
  • Access to a terminal
  • A text file to examine (or use standard input from another command)

Step-by-Step

  1. Open your terminal. On most Linux desktops, press Ctrl + Alt + T or search for "Terminal" in your applications menu. On macOS, open Terminal from Applications > Utilities.
  2. Check the default behavior. Run head /etc/passwd to view the first 10 lines of the system user database. This confirms the command works and shows the default output size.
  3. Specify a different number of lines. Use the -n (or --lines) option followed by a positive integer. For example, head -n 5 /etc/passwd prints only the first 5 lines. You may omit the space: head -5 /etc/passwd works identically.
  4. Read from standard input. Pipe output from another command into head. For instance, ls -la /var/log | head -n 8 lists log files and shows only the first 8 entries. This is useful for inspecting large outputs without scrolling.
  5. Process multiple files. Pass several filenames to head. Run head /etc/hosts /etc/resolv.conf to display the first 10 lines of each file, separated by a header line showing the filename. To suppress headers, add the -q (quiet) option: head -q /etc/hosts /etc/resolv.conf.
  6. Use byte counts instead of lines. With the -c (or --bytes) option, head outputs the first N bytes. For example, head -c 200 /var/log/syslog shows the first 200 characters. This works for binary or text files but may split multi-byte characters (e.g., UTF-8) in the middle.
  7. Combine options safely. You can chain -n and -q but not -n and -c in the same call. Valid: head -n 3 -q file1 file2. Invalid: head -n 5 -c 50 file will cause an error; choose one mode only.
  8. Handle empty or missing files. If a file is empty, head produces no output and exits cleanly. If a file doesn't exist, it prints an error like "No such file or directory" and continues with remaining files if provided.

Common Mistakes

Users often confuse head with tail, which shows the end of a file. Another frequent error is forgetting to quote paths with spaces. For example, head My File.txt fails because the shell interprets it as two arguments. Correct it with quotes: head "My File.txt" or escape spaces: head My\ File.txt.

Some assume -n accepts negative numbers, but head -n -5 is invalid and may produce unexpected behavior or an error. To skip initial lines and show the rest, use tail -n +6 instead.

When using -c, be aware that byte counts count raw bytes, not characters. In UTF-8, accented characters (e.g., é) use two bytes, so head -c 10 might display fewer than 10 visible characters.

Another mistake is expecting head to modify files. It only reads and outputs; it never writes or overwrites. Use redirection (>) to save output to a new file, like head -n 20 largefile.log > summary.txt.

Tips

Use head -n 0 to test if a file exists without printing anything—it returns exit code 0 if the file is readable, even if empty.

When debugging scripts, pipe head early in a chain to spot issues without waiting for full output: find / -name "*.log" 2>/dev/null | head -n 20.

Combine with wc -l to count lines efficiently: head -n 50 data.csv | wc -l confirms exactly 50 lines were processed.

For consistent formatting in scripts, always use absolute paths or ensure relative paths are resolved correctly, especially when head runs from cron jobs or different working directories.

On systems with limited memory, head is safer than tools that load entire files into memory, as it stops reading once the requested lines or bytes are reached.

Troubleshooting

If head returns "Permission denied", the file exists but lacks read permissions. Fix by running with elevated privileges: sudo head /etc/shadow, or adjust permissions with chmod (carefully) if appropriate.

If the command is not found, verify coreutils is installed, as head is part of this package. On Debian/Ubuntu, run sudo apt install coreutils; on RHEL/CentOS, use sudo yum install coreutils.

If output appears truncated mid-line, check for binary content. Use file filename to confirm the type. For binary files, prefer hexdump or xxd instead of head.

When piping, ensure the upstream command completes successfully. If some_command | head -n 10 shows no output, test some_command alone to verify it produces data.

On macOS, the default head behaves like Linux, but if you installed GNU coreutils via Homebrew, use ghead to access it explicitly. Check with which head and head --version.

Frequently Asked Questions

Does head work with compressed files like .gz?

No, head reads raw bytes and cannot decompress files. Use zcat file.gz | head or zless file.gz (if available) to view compressed file contents.

Can head read from stdin interactively?

Yes. Run head -n 3 and type lines manually; press Ctrl + D to signal end-of-input after entering at least three lines.

How do I show exactly one line?

Use head -n 1 filename or head -1 filename. This is commonly used to extract headers from CSV files.

Is head safe for large files?

Yes. It reads only up to the requested amount, making it efficient and low on system resources even for multi-gigabyte files.

What if I need the last N lines instead?

Use tail with the same -n option: tail -n 20 logfile shows the final 20 lines.

Does head support wildcards like *.txt?

Yes, the shell expands wildcards before passing filenames to head. Example: head *.log shows the first 10 lines of each .log file in the current directory.