Head Tail and Cat Commands in Linux



Whether you're examining logs, configuring system settings, or writing scripts, the ability to quickly view and manipulate file content is essential. Three fundamental commands that empower you to do just that are head, tail, and cat. In this tutorial, we will delve into these commands, exploring their functionalities, options, and practical use cases.

Introducing cat: Concatenate and Display Files

The cat command (short for "concatenate") is the most basic yet versatile tool for displaying file content. Its primary function is to read files and print their contents to standard output (your terminal).

Basic Usage of cat Command

To display the contents of a file named myfile.txt ?

cat myfile.txt

cat with Multiple Files

cat can also concatenate multiple files and display them sequentially ?

cat file1.txt file2.txt file3.txt

This will display the contents of file1.txt, followed by file2.txt, and then file3.txt.

Useful cat Options

-n (Number Lines) ? Numbers all output lines ?

cat -n myfile.txt

-b (Number Non-Blank Lines) ? Numbers only non-blank lines ?

cat -b myfile.txt

-s (Suppress Repeated Empty Lines) ? Suppresses consecutive empty lines, replacing them with a single empty line ?

cat -s myfile.txt

-E (Show End of Lines) ? Displays $ at the end of each line, making white-space characters visible ?

cat -E myfile.txt

-T (Show Tabs as ^I) ? Displays tabs as ^I ?

cat -T mynewfile.txt

Using cat for File Creation

cat can also be used to create new files or append to existing ones using redirection ?

  • Creating a new file ?
cat > ourfile.txt
# Type your text, then press Ctrl+D to save
  • Appending to an existing file ?
cat >> existingfile.txt
# Type your text, then press Ctrl+D to save

Introducing head: Display the Beginning of a File

The head command displays the first few lines of a file. By default, it shows the first 10 lines.

Basic Usage of head Command

To display the first 10 lines of myfile.txt ?

head file.txt

Specifying the Number of Lines

Use the -n option to specify the number of lines to display ?

head -n 20 myfile.txt  # Display the first 20 lines
head -20 myfile.txt    # Shorter version, also displays the first 20 lines

Introducing tail: Display the End of a File

The tail command displays the last few lines of a file. Like head, it shows the last 10 lines by default.

Basic Usage of tail Command

To display the last 10 lines of myfile.txt ?

tail file.txt

Specifying the Number of Lines

Use the -n option to specify the number of lines ?

tail -n 5 myfile.txt   # Display the last 5 lines
tail -5 myfile.txt     # Shorter version, also displays the last 5 lines

Following File Changes with tail -f

The -f (follow) option is particularly useful for monitoring log files. It keeps tail running and displays new lines as they are added to the file ?

tail -f /var/log/syslog

Press Ctrl+C to stop following the file.

Combining head, tail, and cat

These commands can be combined using pipes (|) to perform more complex operations.

Example 1: Displaying a Specific Line

To display the 5th line of a file ?

head -n 5 file.txt | tail -n 1

This first extracts the first 5 lines using head, and then extracts the last line (which is the 5th line) from that output using tail.

Example 2: Displaying Lines 10-20

head -n 20 file.txt | tail -n 11

This extracts the first 20 lines and then displays the last 11 of those (lines 10-20 of the original file).

Example 3: Counting Lines in a File

While wc -l is the standard way to count lines, you can also use cat and wc ?

cat file.txt | wc -l

Practical Use Cases

Let us now look at some of the practical use-cases of these commands ?

  • Viewing Log Files ? tail -f is invaluable for monitoring log files in real time.
  • Inspecting Configuration Files ? head and tail can quickly show you the beginning or end of configuration files.
  • Extracting Specific Data ? Combining these commands with other tools like grep and sed allows you to extract specific information from files.
  • Creating Small Files ? cat with redirection is a quick way to create small text files.

Conclusion

head, tail, and cat are essential command-line tools for working with text files in Linux. Mastering these commands will significantly improve your efficiency when navigating and manipulating file content. Their simplicity and versatility make them indispensable for any Linux user, from beginners to seasoned system administrators. They are cornerstones of command-line text processing and are frequently used in shell scripts and other automation tasks.

Updated on: 2025-03-26T15:16:46+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started