Concept Flow - head and tail
Start
Input File
head: Read first N lines
Output first N lines
tail: Read last N lines
Output last N lines
The commands read a file and output either the first or last N lines, showing the start or end of the file.
head -n 3 sample.txt tail -n 2 sample.txt
| Step | Command | Action | Output Lines | Output Content |
|---|---|---|---|---|
| 1 | head -n 3 sample.txt | Reads first 3 lines | 3 | Line1\nLine2\nLine3 |
| 2 | tail -n 2 sample.txt | Reads last 2 lines | 2 | Line4\nLine5 |
| 3 | head -n 3 sample.txt | If file has fewer than 3 lines, outputs all lines | 2 | Line1\nLine2 |
| 4 | tail -n 2 sample.txt | If file has fewer than 2 lines, outputs all lines | 1 | Line1 |
| 5 | head -n 0 sample.txt | Reads zero lines, outputs nothing | 0 | |
| 6 | tail -n 0 sample.txt | Reads zero lines, outputs nothing | 0 |
| Variable | Start | After head -n 3 | After tail -n 2 | After head -n 0 | After tail -n 0 |
|---|---|---|---|---|---|
| Output Lines | N/A | 3 lines | 2 lines | 0 lines | 0 lines |
| Output Content | N/A | Line1\nLine2\nLine3 | Line4\nLine5 |
head and tail commands: head -n N filename: outputs first N lines tail -n N filename: outputs last N lines If file has fewer lines, outputs all lines -n 0 outputs nothing Useful to preview start or end of files