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
head command do by default when run on a text file?headhead command shows the start of a file, by default 10 lines.tail shows the end, but head shows the beginning lines.head = first 10 lines [OK]log.txt?tail shows the end of a file, so it is used to show last lines.-n option-n 5 specifies to show 5 lines. So tail -n 5 log.txt is correct.tail -n 5 shows last 5 lines [OK]data.txt with 20 lines, what will be the output of head -n 3 data.txt | tail -n 2?head -n 3 data.txttail -n 2 on the outputtail -n 2 takes the last 2 lines of the previous output, so lines 2 and 3.head -n 5 file.txt but get an error: head: invalid option -- 'n'. What is the likely cause?invalid option -- 'n', meaning -n is not recognized.head do not support -n and require different syntax.head command version does not support -n -> Option Bapp.log in real-time but only want to see the newest lines as they appear. Which command combination is best?tail with -n 15 and -ftail -n 15 -f app.log shows last 15 lines and follows file changes live.Pass the test and this pattern moves unknown