How to Use the Linux Cat Command?

By | May 28, 2022
How to Use the Linux Cat Command

If you’ve ever interacted with Linux, you’ve certainly come across a code snippet that has used the Cat command. ‘Cat’ is an abbreviation of Concatenate. This command shows the content of one or more files without requiring the file to be opened for modification. Moreover, it also concatenates and creates files. The Cat command goes through the complete data from files and displays it on Unix-like (Linux) operating systems.

To view the contents of a file at the command prompt, the most straightforward technique is to use the Cat command. The blog explains all the ways of using the Cat command in Linux. Take a look at the following prerequisites before moving forward:

  • A Linux-based system
  • The privilege of accessing the terminal window or the command line

What is Cat Command?

In Linux/Unix or other popular operating systems, the Cat command (short for “concatenate”) is one of the most commonly used commands. It is utilized for creating single or multiple files, reading their contents, concatenating files, and redirecting output to the terminals or files. It’s a common Linux tool for concatenating and displaying files.

Files or standard input are concatenated to standard output with the Cat command. It reads standard input when there is no FILE or when FILE is marked as “-“. You can also use the Cat command to rapidly create a file. A cat can read and write data to and from conventional input and output devices. It includes three major functions for working with text files: generating, displaying, and merging them. It can be used further for a variety of purposes, such as:

  • Displaying Text files
  • Copying text files into new documents
  • Combining two text files by appending the contents of one to the end of another.
  • Showing a text file on the screen
  • Making new documents
  • Reading text files
  • Modify the text files
  • Concatenating files

Cat Command Syntax

Follow the below-mentioned format to use the Cat command:

cat [options] filename(s)

[options] – This allows you to give the Cat command additional instructions. Use the –n option, for example, to display the contents of a file with each line numbered:

cat –n filename

filename(s) – Enter the name of the file (or files) to be shown. Every file would be presented if you use more than one filename.

Using Cat command on Linux

Following are the Cat commands that are frequently used in Linux for performing operations. An individual should start their work with these commands, build several sample files, and try out the Cat commands to get a better hands-on experience:

1. Create a New File

Cat command makes the process of creating new files and adding content in it a lot simpler. An individual just has to create two sample files, test1.txt and test2.txt, to use as test files for further instructions.

1. Create the first file in a terminal window:

cat >test1.txt

2. Your cursor will move to a new line, where you can type whatever you like. Make a simple sentence like this:

Here’s my text file #1

3. Press the Ctrl key along with ‘d’ to leave the prompt and save the changes to the file.

For making test2.txt, repeat the cycle. Execute the command below. Right when you’ve finished, press Ctrl+d.

cat >test2.txt

Here’s my text file #2

2. Show the Contents of the Single File

Execute the following command to see the contents of test1.txt using the Cat command:

cat test1.txt

3. Displaying the Content of Multiple Files

Execute the following command to see the contents of both the files:

cat test1.txt test2.txt

4. Redirecting Contents of a Single File

Cat command can save the contents of a file rather than displaying them on the screen.

cat test1.txt > test3.txt

The destination filename needs to be created if it does not already exist. You should see the contents of test1.txt in test3.txt if you run cat on it (redirection):

cat test3.txt

If you’re exporting a file that already exists, the contents of the file will be overwritten:

cat test2.txt > test3.txt

cat test3.txt

5. Redirecting Contents of Multiple Files

You can combine the contents of many files into a single file by using the following command:

cat test1.txt test2.txt > test3.txt

For example, use the following command to display the contents of test3.txt:

cat test3.txt

6. Reversing the order of the contents

The Cat command can reverse the order of a file’s contents (based on lines). To accomplish this, use the tac (reverse of cat) command instead of cat:

tac test3.txt

7. Add the contents of one file to another

The Cat command can be used to append a file’s contents to the end of another file. Use a double “>>” symbol instead of a single “>”:

cat test1.txt >> test3.txt

Run the following command to open the test3 file:

cat test3.txt

8. Text to an Existing File

For appending text to an existing file, use the following command:

cat >> test1.txt

Include the following line within the file:

This is the second line in test file #1.

Hold down Ctrl and press D. Examine the contents of the file test1.txt:

cat test1.txt

9. Combining Operations

The Cat command’s functionality can be mixed. To merge the content of two files and saving the output in new files, run the following code for instance:

cat test1.txt test2.txt > test4.txt

cat test4.txt

You can also attach numerous files to the end of the newly created file:

cat test2.txt test1.txt >> test4.txt

cat test4.txt

It’s worth noting that the sequence in which the files are added to the selected destination file.

10. Managing Large Files

If you’re executing cat on a larger file, you’ll get a long string of data that’s difficult to understand. Therefore, you can use | more: to divide it into several pages. For example:

cat test4.txt | more

At first, you’ll see a single page of the document. It will scroll to the next page when you touch a key. Use | less if you want to be able to scroll forward and backward through the display. For example:

cat test4.txt | less

11. Displaying line numberings

Line numbers make the output extremely handy, especially if you have a huge file. Add the -n option to the Cat command for enabling line numbering:

cat –n test1.txt

Use the -s option to omit repeated empty output lines and suppress repeated empty lines:

cat -s file.txt

12. Showing the Line’s End

With $, you will be able to use the Cat command for highlighting the end of each line and the spaces between them. Execute the below-mentioned command:

cat -e test1.txt

The result shows one $ at the end of the sample file test1.txt because it only has one line.

13. Displaying Tab Separated Lines

The Cat command can show the contents of a file as well as the tab space between the texts. For example, run, display tab-separated lines as follows:

cat -t test4.txt

14. Listing All of The Cat commands

For listing down all the Cat commands on your screen, execute the command mentioned below:

cat ––help

Concatenating Files

When using the Cat command with two or more file names as inputs, the content of the files will get concatenated. cat scans all the files in the order specified in its parameters and shows the contents of those files in the same order.

The following command, for example, will read the contents of files 1.txt and 2.txt and display the result in the terminal:

cat file1.txt file2.txt

An individual is allowed to write a file by concatenating two or more text files. Using the (>) operator, the contents of file1.txt and file2.txt will be concatenated and written to a new file combinedfile.txt:

cat file1.txt file2.txt > combinedfile.txt

If the combinedfile.txt file doesn’t exist, the command will create it. Otherwise, it will overwrite the file. To concatenate the contents of file1.txt and file2.txt and append the result to file3.txt to use the (>>) operator:

cat file1.txt file2.txt >> file3.txt

If the file is not present, it will be created. When concatenating files with Cat, you can use the same arguments as shown in the previous section.

Conclusion

In Linux, the Cat command is one of the most commonly used commands. It is utilized for creating single or multiple files, reading their contents, concatenating files, and redirecting output to the terminals or files.

It’s a common Linux tool for concatenating and displaying files. We hope this blog helped you understand all the important Cat commands to perform various actions related to files.

Leave a Reply

Your email address will not be published.