head Linux Command
Displays the first 10 lines of a file by default. Use -n to specify count.
What does head do?
Displays the first 10 lines of a file by default. Use -n to specify count.
Syntax
head [options] filenamePractical example
head -n 20 file.txtQuestions about head
How do I show the first 5 lines of a file?
Use `head -n 5 filename` or `head -5 filename` to display only the first 5 lines. The default is 10 lines if no number is specified.
How do I show the first N bytes of a file?
Use `head -c 100 filename` to display the first 100 bytes. The `-c` flag specifies byte count instead of line count.
How do I use head with a pipe?
Use `command | head -n 20` to get the first 20 lines of a command's output. For example, `ls | head` shows the first 10 files.
How do I show the first 100 characters?
Use `head -c 100 filename` to show the first 100 bytes/characters. This is useful for previewing binary files or checking file headers.
What is the difference between head and tail?
`head` shows the beginning of a file, while `tail` shows the end. Use `head` for file headers and `tail` for logs or recent entries.
How do I show only the first line of a file?
Use `head -n 1 filename` to display only the first line. This is useful for reading file headers or getting the first record.
How do I combine head and tail?
Use `head -n 20 filename | tail -n 5` to get lines 16-20. First take the first 20 lines, then extract the last 5 of those.
Related File Management
moreViews file contents one screen at a time (forward-only navigation).
tailDisplays the last 10 lines of a file. Use -f to follow file changes in real time.
lessViews file contents page by page with forward and backward navigation.
nanoOpens a simple, beginner-friendly terminal text editor.
catDisplays the entire contents of a file on the terminal.
vimOpens the powerful Vim text editor for editing files.