Linux cat Command: File Concatenating

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial introduces the cat command in Linux, a versatile tool for viewing and manipulating text files. You'll learn how to use cat to display file contents, combine files, and utilize various options to enhance file viewing. By the end of this tutorial, you'll be comfortable using cat for basic file operations, an essential skill for any Linux user.

Imagine you're a new intern at a tech startup. Your first task is to compile a daily report from various text files scattered across the company's Linux server. You'll need to view file contents, combine files, and format the output. The cat command will be your primary tool for this task.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") subgraph Lab Skills linux/cat -.-> lab-210986{{"`Linux cat Command: File Concatenating`"}} linux/redirect -.-> lab-210986{{"`Linux cat Command: File Concatenating`"}} linux/pwd -.-> lab-210986{{"`Linux cat Command: File Concatenating`"}} linux/ls -.-> lab-210986{{"`Linux cat Command: File Concatenating`"}} end

Exploring the Project Directory

Let's start by examining the contents of your project directory.

  1. Open your terminal. You should see a command prompt, typically ending with a $ symbol.

  2. Navigate to the project directory:

cd /home/labex/project

This command changes your current directory to /home/labex/project. The cd command stands for "change directory".

  1. Verify your current location:
pwd

The pwd command stands for "print working directory". It should display /home/labex/project.

  1. List the contents of the directory:
ls

This command will show you all the files and directories in your current location. You should see several text files listed, including daily_report.txt, sales.txt, and marketing.txt.

Viewing File Contents

Now, let's use the cat command to view the contents of a file. The cat command is short for "concatenate", but it's often used to simply display file contents.

  1. View the contents of the file daily_report.txt:
cat daily_report.txt

This command will display the entire contents of the file in your terminal. If the file is long, it may scroll past the visible area of your terminal window.

  1. If you can't see the beginning of the file, you can clear your terminal screen for a fresh start:
clear

The clear command isn't strictly necessary, but it can help keep your workspace tidy. Don't worry about understanding it fully now; you'll learn more about terminal management in future lessons.

  1. Now, view the file contents again:
cat daily_report.txt

Take a moment to read the contents. This is the power of the cat command - it allows you to quickly view the contents of text files directly in your terminal.

Combining Multiple Files

The cat command can also be used to combine multiple files. This is where the "concatenate" part of its name comes from.

  1. First, let's view the contents of both sales.txt and marketing.txt separately:
cat sales.txt
cat marketing.txt

Take note of the contents of each file.

  1. Now, let's combine these files:
cat sales.txt marketing.txt

This command will display the contents of both files, one after the other, as if they were a single file. Notice how cat simply outputs the contents of each file in the order you specify.

  1. Now we're going to introduce a new concept called "output redirection". In Linux, we can take the output of a command (what you normally see printed in the terminal) and send it to a file instead. We do this using the > symbol. Here's how it works:
cat sales.txt marketing.txt > combined_report.txt

Let's break this down:

  • cat sales.txt marketing.txt is the command we've used before to display the contents of both files.
  • The > symbol is new. It tells Linux to take whatever would have been displayed in the terminal and instead write it to a file.
  • combined_report.txt is the name of the new file we're creating.

So, this command is saying: "Take the combined contents of sales.txt and marketing.txt, and instead of showing them to me, put them in a new file called combined_report.txt."

This is a powerful feature in Linux that allows you to save the output of commands for later use. Don't worry if it seems a bit confusing at first - you'll get plenty of practice with it in future lessons.

  1. To make sure our redirection worked, let's verify the contents of the new file:
cat combined_report.txt

You should see the contents of both sales.txt and marketing.txt in this new file. If you do, congratulations! You've successfully used output redirection to combine files.

Using the Line Number Option

The cat command offers options to enhance its output. Let's explore the line number option.

  1. Display the contents of daily_report.txt with line numbers:
cat -n daily_report.txt

The -n option tells cat to number all output lines. This can be very useful when you need to refer to specific lines in a file.

  1. Compare this to the output without line numbers:
cat daily_report.txt

Notice how the -n option adds a number at the beginning of each line, making it easier to reference specific parts of the file.

Displaying End of Line Characters

Another useful option is to display end-of-line characters. This can help you identify issues with line endings or trailing spaces.

  1. View the contents of daily_report.txt with end-of-line characters visible:
cat -E daily_report.txt

The -E option tells cat to display a $ at the end of each line. This makes it easy to see where each line ends, which can be particularly useful when dealing with formatting issues.

  1. Compare this to the regular output:
cat daily_report.txt

Notice how the -E option adds a $ at the end of each line, making line endings explicit.

Summary

In this tutorial, you learned how to use the cat command for various file operations:

  • Viewing file contents
  • Combining multiple files
  • Using options like line numbers (-n) and end-of-line display (-E)

These skills will be invaluable for managing and manipulating text files in Linux environments. As you continue your Linux journey, you'll find many more uses for the versatile cat command.

Additional cat command options not covered in this tutorial include:

  • -A: Show all non-printing characters (equivalent to -vET)
  • -b: Number non-blank output lines
  • -s: Suppress repeated empty output lines
  • -T: Display TAB characters as ^I
  • -v: Use ^ and M- notation, except for LFD and TAB

Resources

Other Linux Tutorials you may like