Linux cat Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux cat command, which is a versatile tool for concatenating and displaying the contents of text files. You will start by understanding the purpose and syntax of the cat command, including its commonly used options. Then, you will practice concatenating and displaying the contents of multiple text files using the cat command. Finally, you will learn how to append text to existing files using the cat command.

The lab covers the basic file and directory operations in Linux, which are essential skills for system administrators and developers working in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/more("`File Scrolling`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") subgraph Lab Skills linux/cat -.-> lab-422589{{"`Linux cat Command with Practical Examples`"}} linux/more -.-> lab-422589{{"`Linux cat Command with Practical Examples`"}} linux/echo -.-> lab-422589{{"`Linux cat Command with Practical Examples`"}} linux/redirect -.-> lab-422589{{"`Linux cat Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the cat Command

In this step, you will learn about the purpose and basic syntax of the cat command in Linux. The cat command is a versatile tool that allows you to concatenate and display the contents of text files.

The basic syntax of the cat command is as follows:

cat [options] [file(s)]

Here, [options] represents any optional flags or parameters you can use with the cat command, and [file(s)] represents the file(s) you want to concatenate and display.

Some common options for the cat command include:

  • -n: Displays the output with line numbers.
  • -E: Displays a $ character at the end of each line.
  • -s: Squeezes multiple adjacent empty lines into one.

To see the cat command in action, let's create a sample text file and use the cat command to display its contents.

## Create a sample text file
echo "This is the first line." > sample.txt
echo "This is the second line." >> sample.txt
echo "This is the third line." >> sample.txt

## Display the contents of the file using cat
cat sample.txt

Example output:

This is the first line.
This is the second line.
This is the third line.

As you can see, the cat command simply displays the contents of the sample.txt file.

Concatenate and Display Text Files

In this step, you will learn how to use the cat command to concatenate and display the contents of multiple text files.

Let's create two more sample text files:

## Create additional sample text files
echo "This is the first line of file1.txt." > file1.txt
echo "This is the second line of file1.txt." >> file1.txt

echo "This is the first line of file2.txt." > file2.txt
echo "This is the second line of file2.txt." >> file2.txt

Now, you can use the cat command to display the contents of these files:

## Display the contents of file1.txt
cat file1.txt

## Display the contents of file2.txt
cat file2.txt

Example output:

This is the first line of file1.txt.
This is the second line of file1.txt.
This is the first line of file2.txt.
This is the second line of file2.txt.

To concatenate the contents of multiple files, you can simply list the file names as arguments to the cat command:

## Concatenate the contents of file1.txt and file2.txt
cat file1.txt file2.txt

Example output:

This is the first line of file1.txt.
This is the second line of file1.txt.
This is the first line of file2.txt.
This is the second line of file2.txt.

As you can see, the cat command has combined the contents of file1.txt and file2.txt into a single output.

Append Text to Existing Files

In this step, you will learn how to use the cat command to append text to existing files.

Let's start by creating a new file called file3.txt with some initial content:

## Create file3.txt with initial content
echo "This is the first line of file3.txt." > file3.txt

Now, you can use the cat command to append additional text to the file:

## Append text to file3.txt
cat >> file3.txt
This is the second line of file3.txt.
This is the third line of file3.txt.

Press Ctrl+D to save the changes and exit the cat command.

You can now verify the contents of file3.txt:

## Display the contents of file3.txt
cat file3.txt

Example output:

This is the first line of file3.txt.
This is the second line of file3.txt.
This is the third line of file3.txt.

As you can see, the cat command has appended the new lines of text to the existing file3.txt file.

You can also use the >> operator to append text to a file directly from the command line:

## Append text to file3.txt using the >> operator
echo "This is the fourth line of file3.txt." >> file3.txt

Now, let's verify the contents of file3.txt again:

## Display the contents of file3.txt
cat file3.txt

Example output:

This is the first line of file3.txt.
This is the second line of file3.txt.
This is the third line of file3.txt.
This is the fourth line of file3.txt.

The cat command has successfully appended the new line of text to the existing file3.txt file.

Summary

In this lab, you learned about the purpose and basic syntax of the cat command in Linux, which is a versatile tool for concatenating and displaying the contents of text files. You created sample text files and used the cat command to display their contents, as well as learned about common options like -n, -E, and -s to customize the output. Additionally, you learned how to use the cat command to concatenate and display the contents of multiple text files.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like