How to Use the “cat” Command in Bash?

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.

The text that we wrote in our cat1.txt file is shown in the following image:

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.

The contents of our two files will be displayed in separate lines, as shown in the following image:

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.

The contents of our two files will be displayed on the terminal separated by a “$” symbol as shown in the following image:

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.

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.

Now you can also check if a file with the desired text has been created or not by running this command:

$ cat SampleFile


We have replaced SampleFile with cat3 to display its contents, as shown in the image below:

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


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:

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.”

Running this command will simply append the contents of one file to another without displaying anything on the terminal.

You can confirm it by checking the contents of SampleFile2 or cat2.txt in our case as follows:

$ cat SampleFile2.txt


The contents of our appended file i.e. cat2.txt are shown in the image below:

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.



from Linux Hint https://ift.tt/3lNBksh

Post a Comment

0 Comments