Jump to content

How to Use the “cat” Command in Bash?


Linux Hint

Recommended Posts

The “cat” command in Bash stands for “concatenate”. This command is very frequently used for viewing, creating, and appending files in Linux. Today, we will explain to you the usage of this command in Bash in Linux Mint 20 by sharing some examples.

Method of Using the “cat” Command in Bash:

For using the “cat” command in Bash, we will show you six different examples. For demonstrating these examples, we have created two different text files named cat1.txt and cat2.txt with some random text. We will be using these text files in our examples below:

Example # 1: Displaying the Contents of a File:

If you want to display the contents of a file with the “cat” command in Bash, we have to proceed by executing this command:

$ cat SampleFile

Here, you have to replace SampleFile with the name of the file which you want to be displayed on the terminal. We wanted to display the contents of the cat1.txt file that is why we have replaced SampleFile with cat1.txt.
Picture1-3.png
The text that we wrote in our cat1.txt file is shown in the following image:
Picture2-3.png

Example # 2: Displaying the Contents of Multiple Files:

If you have multiple files and you want to display the contents of all these files at once, then you have to proceed by executing this command:

$ cat SampleFile1 SampleFile2

Here, you have to replace SampleFile1 and SampleFile2 with the names of the files whose contents you want to be displayed on your terminal. We wanted to display the contents of the cat1.txt and cat2.txt files; that is why we have replaced SampleFile1 with cat1.txt and SampleFile2 with cat2.txt. You can also have more than two file names in this command if you want to display the contents of more than two files on the terminal.
Picture3-2.png
The contents of our two files will be displayed in separate lines, as shown in the following image:
Picture4-2.png

Example # 3: Displaying the Contents of Multiple Files Separated by a “$” Symbol:

You can see from the example shown above that we can display the contents of more than one file at once on our terminal. However, there is no clear distinction or separation in the file contents. In other words, you cannot distinguish which contents belong to which file. Therefore, we can use the “$” separator, which will be inserted at the end of the contents of each file. In this way, you will clearly be able to see where the content of one file ends.

For doing this, we will have to use the “-e” flag in the following manner:

$ cat –e SampleFile1 SampleFile2

Here, you have to replace SampleFile1 and SampleFile2 with the names of the files whose contents you want to be displayed separated by a “$” symbol on your terminal. We wanted to display the contents of the cat1.txt and cat2.txt files separated by a “$” symbol; that is why we have replaced SampleFile1 with cat1.txt and SampleFile2 with cat2.txt. You can also have more than two file names in this command if you want to display the contents of more than two files separated by a “$” symbol on the terminal.
Picture5-2.png
The contents of our two files will be displayed on the terminal separated by a “$” symbol as shown in the following image:
Picture6-2.png

Example # 4: Creating a New File with the “cat” command:

Other than displaying the contents of a file, the “cat” command is also capable of creating a new file. For that, we need to proceed by executing this command:

$ cat >SampleFile

Here, you have to replace SampleFile with the name of the file that you want to create with the “cat” command. We wanted to create a file named cat3, that is why we have replaced SampleFile with cat3.
Picture7-1.png
When you press the Enter key to execute this command, you will see a prompt on your terminal, which will indicate that the “cat” command is asking you to enter some text into your file. You can add any text of your choice in it as we did and then press the Ctrl+ d key combination to save the newly created file.
Picture8-1.png
Now you can also check if a file with the desired text has been created or not by running this command:

$ cat SampleFile

Picture9-1.png
We have replaced SampleFile with cat3 to display its contents, as shown in the image below:
Picture10-1.png

Example # 5: Displaying Line Numbers along with the Contents of a File:

If you have a very large file, then it is always convenient to view it with line numbers. To display the line numbers along with the contents of a file, you have to proceed by executing this command:

$ cat –n SampleFile

Picture11-1.png
We have replaced SampleFile with cat3 since we wanted this file’s contents to be displayed with line numbers, as shown in the image below:
Picture12-1.png

Example # 6: Appending the Contents of Files:

If you ever want to append the contents of one file at the end of another, then you can proceed by executing this command:

$ cat SampleFile1 >> SampleFile2

Here, you have to replace SampleFile1 with the name of the file whose contents you want to append and SampleFile2 with the name of the file where you want to append the contents. We have replaced SampleFile1 with cat1.txt and SampleFile2 with cat2.txt. It means that our text “Hello World!” will be appended at the end of our text, “We want to test the “cat” command.”
Picture13-1.png
Running this command will simply append the contents of one file to another without displaying anything on the terminal.
Picture14-1.png
You can confirm it by checking the contents of SampleFile2 or cat2.txt in our case as follows:

$ cat SampleFile2.txt

Picture1-4.png
The contents of our appended file i.e. cat2.txt are shown in the image below:
Picture2-4.png

Conclusion:

This is how you can learn the usage of the “cat” command in Linux Mint 20 by going through all the examples that we discussed today.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...