head and tail in Linux CLI - Time & Space Complexity
When using head and tail commands, it's helpful to know how their execution time changes as the input file grows.
We want to understand how the number of lines in a file affects how long these commands take to run.
Analyze the time complexity of the following commands.
head -n 10 largefile.txt
tail -n 10 largefile.txt
These commands print the first or last 10 lines of a file named largefile.txt.
Look at what the commands do repeatedly.
- Primary operation: Reading lines from the file.
- How many times: For
head, it reads only the first 10 lines. Fortail, it seeks to the end of the file and reads backwards only the necessary amount to find the last 10 lines.
As the file gets bigger, how does the work change?
| Input Size (lines) | Approx. Operations for head | Approx. Operations for tail |
|---|---|---|
| 10 | Reads 10 lines | Reads 10 lines |
| 100 | Reads 10 lines | Reads ~10 lines from end |
| 1000 | Reads 10 lines | Reads ~10 lines from end |
Pattern observation: Both head and tail perform a fixed amount of work regardless of file size.
Time Complexity: O(1) for head and O(k) for tail, where k is the number of lines requested.
This means head takes constant time, and tail takes time proportional to the number of lines requested, which is usually small and treated as constant.
[X] Wrong: "tail reads through the whole file to find the last lines, making it O(n)."
[OK] Correct: In modern implementations (like GNU tail on Linux), tail seeks to the end of the file and reads backwards only enough data to identify and output the last 10 lines, making it also O(k), which is effectively constant for small k.
Understanding how simple commands like head and tail behave with large files shows you can think about efficiency in everyday tasks, a skill valuable in many scripting and automation roles.
What if we used tail -n 10 on a file stored on a slow network drive? How might that affect the time complexity or perceived speed?
Practice
head command do by default when run on a text file?Solution
Step 1: Understand the default behavior of
Theheadheadcommand shows the start of a file, by default 10 lines.Step 2: Compare with other options
tailshows the end, butheadshows the beginning lines.Final Answer:
Shows the first 10 lines of the file -> Option DQuick Check:
head= first 10 lines [OK]
- Confusing head with tail
- Thinking head deletes lines
- Assuming head counts lines
log.txt?Solution
Step 1: Identify the command to show last lines
tailshows the end of a file, so it is used to show last lines.Step 2: Use correct syntax with
The option-noption-n 5specifies to show 5 lines. Sotail -n 5 log.txtis correct.Final Answer:
tail -n 5 log.txt -> Option AQuick Check:
tail -n 5shows last 5 lines [OK]
- Using head instead of tail
- Omitting -n option
- Using tail without -n for line count
data.txt with 20 lines, what will be the output of head -n 3 data.txt | tail -n 2?Solution
Step 1: Understand
This command outputs the first 3 lines: lines 1, 2, and 3.head -n 3 data.txtStep 2: Apply
tail -n 2on the outputtail -n 2takes the last 2 lines of the previous output, so lines 2 and 3.Final Answer:
Lines 2 and 3 of data.txt -> Option AQuick Check:
head 3 then tail 2 = lines 2 & 3 [OK]
- Assuming tail works on whole file, not head output
- Confusing line numbers
- Thinking tail -n 2 shows first 2 lines
head -n 5 file.txt but get an error: head: invalid option -- 'n'. What is the likely cause?Solution
Step 1: Analyze the error message
The error saysinvalid option -- 'n', meaning-nis not recognized.Step 2: Understand command version differences
Some older or non-GNU versions ofheaddo not support-nand require different syntax.Final Answer:
Theheadcommand version does not support-n-> Option BQuick Check:
Invalid option error means unsupported flag [OK]
- Assuming file missing causes option error
- Trying to run as root unnecessarily
- Using wrong syntax without checking version
app.log in real-time but only want to see the newest lines as they appear. Which command combination is best?Solution
Step 1: Understand the need to see last lines and follow updates
You want the last 15 lines and to watch new lines as they are added.Step 2: Use
tailwith-n 15and-ftail -n 15 -f app.logshows last 15 lines and follows file changes live.Final Answer:
tail -n 15 -f app.log -> Option CQuick Check:
tail -n 15 -f = last 15 lines + live updates [OK]
- Using head which shows start, not end
- Omitting -f to follow updates
- Using head -f which is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
