Display the first lines of files
The head command displays the first lines of files. By default, it shows the first 10 lines of each file, making it useful for quickly previewing file contents, examining log files, or processing text data.
| Option | Description |
|---|---|
-n NUM |
Display first NUM lines (default: 10) |
-c NUM |
Display first NUM bytes |
-q |
Quiet mode - never print headers |
-v |
Verbose mode - always print headers |
--help |
Display help information |
--version |
Display version information |
head instead of opening large files in editorsgrep for pattern matching in file headers-c) for binary file inspection-c option-n option followed by the number of lines: head -n 20 filename. You can also use the shorthand format: head -20 filename.
-q to suppress headers or -v to always show them.
-n option specifies the number of lines to display, while -c specifies the number of bytes (characters) to display. Use -c when you need precise byte-level control or when working with binary files.
ls -la | head -n 5 shows the first 5 lines of directory listing. You can also pipe head's output to other commands: head -n 20 file.txt | grep "pattern".