head
Files and directories
Usage
head -n 20 [file]Prints the start of a file; give it several files and it inserts a header for each unless you pass -q.
Common flags
| Flag | Meaning |
|---|---|
| -n | Print the first N lines |
| -c | Print the first N bytes |
| -q | Never print the file name header |
| -v | Always print the file name header |
| -n -5 | GNU: everything except the last 5 lines |
Examples
head -n 5 data.csvHeader row plus four records.
head -c 100 file.binThe first 100 bytes only.
Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does head do?
Prints the start of a file; give it several files and it inserts a header for each unless you pass -q.
Q. How do I type it?
head -n 20 [file] — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
5 are listed here; the full set is in man head. This command sits under Files and directories.
Related commands
man page: man head