0
0
Linux CLIscripting~15 mins

head and tail in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Using head and tail Commands in Linux CLI
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use an empty echo between the two commands to separate outputs.