1. Overview

The cat command is a command-line tool in Linux used for various file-related operations. Further, these operations allow users to print, view, create, concatenate, merge, and manipulate file contents.

In this tutorial, we’ll explore the various functionalities of the cat command, such as displaying file contents, creating new files, and combining multiple files. Additionally, we’ll also explore more advanced usage of the cat command.

2. Common cat Command Options

The basic syntax of the cat command is pretty straightforward:

$ cat [OPTION] [FILE]

Let’s see the meanings of each of the components of the cat command:

  • [OPTION]: represents the various command-line options of the cat command
  • [FILE]: represents the name(s) of the file(s) to be processed

Furthermore, let’s explore various options associated with the cat command:

Options Description
-A Displays all characters
-b, –number-nonblank Specifies line numbers for lines containing code content only, overriding -n
-e Displays non-printing characters with -vE
-E, –show-ends Adds ‘$’ at the end of each line
-n Numbers all output lines
-s, –squeeze-blank Condenses consecutive empty lines
-t Shows tabs using -vT
-v, –show-nonprinting Representing non-printing characters using ^ and M-notation
-T, –show-tabs Shows TAB characters as ^I

With these various options, we can tailor the behavior of the cat command to suit specific needs.

3. Common cat Command Examples

Let’s dive into a practical example of using the cat command.

3.1. Display Contents of a Single File

This is the most basic use of the cat command — to display the content of a file in the terminal.

For example, let’s display the content of a file called test.txt:

$ cat test.txt
This is a test file for learning purposes

3.2. Create a New File

Additionally, we employ the cat command to create a new file and input text into it directly from the command line. Notably, we achieve this by redirecting the input using the > symbol followed by the name of the new file.

For example, let’s create a new file called newfile.txt with some text content:

$ cat > newfile.txt
This is the content of the new file.
It can contain multiple lines.

After executing this command, the cursor moves to a new line in the terminal, indicating that we can start typing the content of the new file. We then press Ctrl+D to save the changes and exit.

3.3. Display Contents of Multiple Files

Furthermore, we can also use the cat command to display the contents of multiple files at once. In particular, this aids in combining the contents of several files in the terminal.

To do this, we list the names of the files we want to view after the cat command, separating them by spaces. Notably, the content of each file displays sequentially in the order listed on the command.

For example, let’s display the content of two files called file1.txt and file2.txt:

$ cat file1.txt file2.txt
This is the content of the first file
this is the content of the second file

This command outputs the combined content of file1.txt followed by file2.txt in the terminal.

3.4. Redirect Contents of a Single File

Moving forward, we can use the cat command to redirect the contents of a single file. This allows the content to be transferred from one file to another.

To achieve this, we use the > symbol followed by the name of the new file. Further, this transfers the output of the cat command to be saved into a specified file.

For example, let’s redirect the content of a file called current.txt to a new file called future.txt using the cat command:

$ cat current.txt > future.txt

This effectively copies the content of the first file to the second file.

Additionally, we can also use the cat command to concatenate multiple files and redirect the combined content into a new file.

For example, let’s concatenate the contents of two files, file1.txt, and file2.txt, and redirect the combined content to a new file called combined.txt:

$ cat file1.txt file2.txt > combined.txt

The command combines the content of the two files and saves the merged content into the new file combined.txt.

3.5. Append Content to a File

Furthermore, we can append content to an existing file using the cat command. To achieve this, we use the >> symbol followed by the name of the file to which we want to append the content.

For example, suppose we already have a file called test.txt with some content. We can append additional text to the file using the cat command:

$ cat >> existing.txt
This is the new line we want to append.

After running the command, the cursor moves to a new line. Then, we enter the text we want to append to the file. Finally, we press Ctrl+D to save the changes and exit.

4. Advanced cat Command Examples

Now, let’s delve into more advanced usage scenarios of the cat command to understand its capabilities further.

4.1. Display Content of All Files of a Specific Type

Furthermore, we can use the cat command to display the content of all files of a specific type within a folder in Linux. In particular, this is useful for quick inspection of the contents of multiple files of a particular type, such as log files or text files.

To successfully achieve this, we use wildcards with the cat command to specify the files to be displayed.

For example, let’s use the cat command to display the contents of all the text files in the current directory:

$ cat *.txt
this is a test file for learning purposes
content 1
content 2
..

The command displays all the content of the text files in the current directory.

4.2. Display Line Numbers With File Content

Lastly, we can also use the cat command to display the content of a file with the line numbers. This is helpful when we need to reference lines of a file or analyze its structure.

Here, we use the -n option with the cat command to display the line numbers of the file.

For example, let’s display the content of the file test.txt with line numbers:

$ cat -n test.txt
     1  This is a test file for learning purposes
     2  The blank line has been removed

As seen in the output, it displays the file’s content with numbers added before each line.

5. Conclusion

In this article, we’ve explored various practical examples and advanced usage scenarios of the cat command in Linux. These range from basic file manipulation tasks to more sophisticated operations, such as displaying line numbers with file content.

Understanding these functionalities allows us to efficiently use the cat command to streamline file-handling processes.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments