Linux for Developers
/Beginner
head & tail
Definition
Commands used to output a specific number of lines from the beginning (`head`) or the end (`tail`) of a file. By default, they print 10 lines.
Explain Like I'm New
Snipping the top or bottom off a document. If you just want to see the most recent error at the very bottom of a log file, you use `tail`.
Real World Example
A server crashes. The developer types `tail -n 20 error.log` to see the very last 20 things that happened before the crash, completely ignoring the thousands of lines above it.
Common Use Cases
- •Checking recent log entries
- •Previewing CSV file headers
Crucial Command Flags
| Flag / Option | Description |
|---|---|
| -n [num] | Output the first/last [num] lines (default is 10). |
| -c [bytes] | Output the first/last [bytes] bytes. |
| -f (tail only) | Follow. Output appended data as the file grows. |
Interview Questions
basic
- If you want to view the first 5 lines of a file, what command do you type?
intermediate
- What does the highly useful `tail -f` command do?