📖 Scenario: You are working with a text file that contains a list of daily sales records for a small shop. You want to quickly check the first few and last few lines of this file to understand the data without opening the entire file.
🎯 Goal: Learn how to use the head and tail commands to view the beginning and end of a text file in the Linux command line.
📋 What You'll Learn
Create a text file named sales.txt with specific lines
Use head command to view the first 3 lines
Use tail command to view the last 2 lines
Print the outputs of both commands
💡 Why This Matters
🌍 Real World
Checking the start and end of log files or data files quickly without opening the entire file saves time and helps find important information fast.
💼 Career
System administrators, developers, and data analysts often use head and tail commands to inspect files and troubleshoot issues efficiently.
Progress0 / 4 steps
1
Create the sales.txt file
Create a file named sales.txt with these exact lines, each on its own line: Monday: 150 Tuesday: 200 Wednesday: 180 Thursday: 220 Friday: 170
Linux CLI
Hint
Use echo -e with newline characters \n and redirect output to sales.txt.
2
Set the number of lines to view
Create two variables: head_lines set to 3 and tail_lines set to 2 to specify how many lines to view from the start and end of the file.
Linux CLI
Hint
Use simple variable assignment like head_lines=3.
3
Use head and tail commands with variables
Use the head command with -n option and the variable head_lines to show the first 3 lines of sales.txt. Then use the tail command with -n and tail_lines to show the last 2 lines.
Linux CLI
Hint
Use head -n $head_lines sales.txt and tail -n $tail_lines sales.txt.
4
Print the outputs of head and tail
Print the output of the head command first, then print a blank line, then print the output of the tail command. Use the variables head_lines and tail_lines in the commands.
Linux CLI
Hint
Use an empty echo between the two commands to separate outputs.
Practice
(1/5)
1. What does the head command do by default when run on a text file?
easy
A. Counts the number of lines in the file
B. Shows the last 10 lines of the file
C. Deletes the first 10 lines of the file
D. Shows the first 10 lines of the file
Solution
Step 1: Understand the default behavior of head
The head command shows the start of a file, by default 10 lines.
Step 2: Compare with other options
tail shows the end, but head shows the beginning lines.
Final Answer:
Shows the first 10 lines of the file -> Option D
Quick Check:
head = first 10 lines [OK]
Hint: Remember: head = start, tail = end of file [OK]
Common Mistakes:
Confusing head with tail
Thinking head deletes lines
Assuming head counts lines
2. Which of the following is the correct syntax to display the last 5 lines of a file named log.txt?
easy
A. tail -n 5 log.txt
B. head -n 5 log.txt
C. tail 5 log.txt
D. head 5 log.txt
Solution
Step 1: Identify the command to show last lines
tail shows the end of a file, so it is used to show last lines.
Step 2: Use correct syntax with -n option
The option -n 5 specifies to show 5 lines. So tail -n 5 log.txt is correct.
Final Answer:
tail -n 5 log.txt -> Option A
Quick Check:
tail -n 5 shows last 5 lines [OK]
Hint: Use -n with tail to specify number of lines [OK]
Common Mistakes:
Using head instead of tail
Omitting -n option
Using tail without -n for line count
3. Given a file data.txt with 20 lines, what will be the output of head -n 3 data.txt | tail -n 2?
medium
A. Lines 2 and 3 of data.txt
B. Lines 1 and 2 of data.txt
C. Lines 18 and 19 of data.txt
D. Lines 3 and 4 of data.txt
Solution
Step 1: Understand head -n 3 data.txt
This command outputs the first 3 lines: lines 1, 2, and 3.
Step 2: Apply tail -n 2 on the output
tail -n 2 takes the last 2 lines of the previous output, so lines 2 and 3.
Final Answer:
Lines 2 and 3 of data.txt -> Option A
Quick Check:
head 3 then tail 2 = lines 2 & 3 [OK]
Hint: Pipe head then tail to slice lines from start [OK]
Common Mistakes:
Assuming tail works on whole file, not head output
Confusing line numbers
Thinking tail -n 2 shows first 2 lines
4. You run head -n 5 file.txt but get an error: head: invalid option -- 'n'. What is the likely cause?
medium
A. The file file.txt does not exist
B. The head command version does not support -n
C. You need to use --lines=5 instead of -n 5
D. You must run the command as root
Solution
Step 1: Analyze the error message
The error says invalid option -- 'n', meaning -n is not recognized.
Step 2: Understand command version differences
Some older or non-GNU versions of head do not support -n and require different syntax.
Final Answer:
The head command version does not support -n -> Option B
Quick Check:
Invalid option error means unsupported flag [OK]
Hint: Check command version if options cause errors [OK]
Common Mistakes:
Assuming file missing causes option error
Trying to run as root unnecessarily
Using wrong syntax without checking version
5. You want to monitor the last 15 lines of a growing log file app.log in real-time but only want to see the newest lines as they appear. Which command combination is best?
hard
A. head -n 15 app.log
B. tail -n 15 app.log
C. tail -n 15 -f app.log
D. head -n 15 -f app.log
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 tail with -n 15 and -f
tail -n 15 -f app.log shows last 15 lines and follows file changes live.
Final Answer:
tail -n 15 -f app.log -> Option C
Quick Check:
tail -n 15 -f = last 15 lines + live updates [OK]
Hint: Use tail -f to follow file changes live [OK]
Common Mistakes:
Using head which shows start, not end
Omitting -f to follow updates
Using head -f which is invalid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.