0
0
Linux CLIscripting~20 mins

head and tail in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Head and Tail Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of head command with line count
What is the output of the command head -n 3 sample.txt if sample.txt contains these lines?
Line1
Line2
Line3
Line4
Line5
Linux CLI
head -n 3 sample.txt
A
Line4
Line5
B
Line3
Line4
Line5
C
Line1
Line2
Line3
Line4
Line5
D
Line1
Line2
Line3
Attempts:
2 left
💡 Hint
head shows the first lines of a file.
💻 Command Output
intermediate
1:30remaining
Output of tail command with byte count
What is the output of tail -c 5 sample.txt if sample.txt contains exactly HelloWorld?
Linux CLI
tail -c 5 sample.txt
AHelloWorld
BHello
CWorld
DdlroW
Attempts:
2 left
💡 Hint
tail -c shows last bytes, counting characters.
🔧 Debug
advanced
2:00remaining
Effect of tail -n with negative number
You run tail -n -3 sample.txt. What happens?
Choose the correct statement.
Linux CLI
tail -n -3 sample.txt
AThe command syntax is correct; no error should occur.
BNegative numbers are not allowed with -n option in tail.
CThe -n option requires a positive integer or zero.
DThe file sample.txt does not exist.
Attempts:
2 left
💡 Hint
Check tail manual for allowed values of -n. Negative numbers indicate 'all but last N lines'.
🧠 Conceptual
advanced
2:00remaining
Combining head and tail to get middle lines
You want to extract lines 4 to 6 from a file named data.txt. Which command correctly does this?
Ahead -n 6 data.txt | tail -n 3
Btail -n 6 data.txt | head -n 3
Ctail -n 3 data.txt | head -n 6
Dhead -n 3 data.txt | tail -n 6
Attempts:
2 left
💡 Hint
Use head to get first 6 lines, then tail to get last 3 of those.
🚀 Application
expert
2:30remaining
Script to monitor last 10 lines of a growing log file
You want to write a script that continuously shows the last 10 lines of app.log as it grows, updating in real-time. Which command achieves this?
Atail -f -n 10 app.log
Bhead -f -n 10 app.log
Ctail -n 10 app.log
Dhead -n 10 app.log
Attempts:
2 left
💡 Hint
Use tail with follow option to watch file growth.