Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.

LPI Linux Essentials Exam 010-160 - Topic 3.2 - Viewing Data with cat, head & tail


When working with text files in Linux, a few commands almost always come into play. Among them, cat, head, and tail are frequently used for a variety of text manipulation tasks directly from the terminal. This guide aims to give you a comprehensive understanding of these commands, supplemented with a shared practice file for hands-on exercises.

Preparing a Practice File

First, let's create a practice file that you'll use throughout this guide for hands-on exercises. Execute the following command in your terminal:

echo -e "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n\n\n\n\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10\nLine 11\nLine 12\nLine 13\nLine 14\nLine 15\nLine 16\nLine 17\nLine 18\nLine 19\nLine 20" > practice_file.txt

This command will generate a text file named practice_file.txt containing 24 lines, 20 of which state a line number and 4 lines in a row that are blank. This file will used in the later cat, head, and tail exercises.

cat Command

The cat command is short for concatenate. It is predominantly used for displaying file contents, but its functionality extends beyond that. It can also append text to existing ones, and combine multiple files together.

Use Cases:

  1. Displaying the content of files
  2. Concatenating multiple files together

Common Options:

Option Description
-n Numbers all output lines
-s Suppresses repeated empty output lines


Use cat to display the contents of practice_file.txt:

cat practice_file.txt

Concatenate practice_file.txt with another file, /etc/hosts:

cat practice_file.txt /etc/hosts > combined_file.txt

View the contents of the new file combined_file.txt.

cat combined_file.txt

Add line numbers to the cat command output:

cat -n practice_file.txt

Suppress repeated empty blank lines:

cat -s practice_file.txt

head Command

The head command is utilized to read the beginning portions of text files. By default, it displays the first 10 lines, but this can be customized. It is especially useful when you wish to inspect large files without the need for a text editor.

Use Cases:

  1. Quickly viewing the initial part of large files
  2. Extracting the starting lines of files
  3. In pipelines for text manipulations

Common Options:

Option Description
-c [num] Shows the first [num] bytes of a file
-n [num] Shows the first [num] lines of a file


Use head to display the first 10 lines of practice_file.txt:

head practice_file.txt

Display only the first 5 lines of practice_file.txt:

head -n 5 practice_file.txt

Show the first 15 bytes of practice_file.txt:

head -c 15 practice_file.txt

tail Command

The tail command essentially serves as the counterpart to head, displaying the end lines of a file. By default, the command shows the last 10 lines, but you can specify a different amount. The tail command can also monitor a file for new content in real-time, which is particularly helpful for keeping an eye on log files.

Use Cases:

  1. Real-time monitoring of log files
  2. Viewing the end lines of files
  3. Text manipulation in pipelines

Common Options:

Option Description
-c [num] Displays the last [num] bytes of a file
-f Follows the file and displays new lines as they appear
-n [num] Displays the last [num] lines of a file


Use tail to display the last 10 lines of practice_file.txt:

tail practice_file.txt

Show only the last 5 lines of practice_file.txt:

tail -n 5 practice_file.txt

Show the last 15 bytes of practice_file.txt:

tail -c 15 practice_file.txt

Follow practice_file.txt in real-time, observing new lines as they are added:

Create a process that will update practice_file.txt with new lines periodically.

until [ $count -eq 100 ]; do
  echo count = $count >> practice_file.txt
  sleep 1
  count=$((count + 1))
done &

Watch as the file is updated every second:

tail -f practice_file.txt

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.