If you have ever used Linux, you have undoubtedly come across some code that makes How to Use Linux Cat Command? Concatenate is abbreviated as CAT. Without opening the file for editing, this command shows the contents of one or more files.
Learn how to use the cat command in Linux in this article.
How to Use Linux Cat Command? Use the following syntax to use the cat command:
filename cat [options] (s)[options] – This allows you to give the cat command additional instructions. Use the -n option, for instance, to show a file’s contents with each line’s number displayed:
filename cat -nEnter the name of the file (or files) you want to display in the filename field. If you use more than one filename, each file will be displayed.
This article lists 15 cat commands along with usage examples. Create a few test files and run the cat commands given below to test the commands.
Using the cat command, you can make new files and add content to them. Make the example files test1.txt and test2.txt so that you can use them to test the other instructions.
1: Launch the terminal and make the first file:
txt, cat2: The desired text can be added by moving the cursor to a new line. Write a short sentence like this:
First test file, this one.Use the cat command to generate a new file.4: Carry out step 4 again to produce test2.txt. Run:
txt, cat5: Type:
Test file #2 is this one.6: Hit Ctrl+d.
Run the following command to use cat to display the contents of test1.txt:
feline test1.txtThe content appears in the output as shown in the image below.
Run the following command to see what both files contain:
txt, test1.txt, catA cat can store the contents of a file in a file as opposed to showing them on the screen.
txt > test3.txt in a catThe destination filename will be created if it doesn’t already exist. You should see the contents of test1.txt if you run cat on test3.txt:
fel test3.txtThe result shows:
txt > test3.txt in a catfel test3.txtThe following text is now present in the test3.txt file:
You can combine the contents of several files into one:
if (cat test1.txt, test2, test3)Show test3.txt’s content by using:
fel test3.txtThe screenshot below shows how the output displays the contents of both files.
How to Use Linux Cat Command? Reverse order can be displayed for a file’s content using the cat command (by lines). Use tac (cat in reverse) to accomplish this:
txt tacThe cat command has the ability to append a file’s contents to the end of another file. Use a double >> symbol rather than a single > sign:
txt after cat test1.txtthe test3 file by executing:
fel test3.txtTest 3’s material should appear first, followed by Test 1’s content.
A similar command can be used to add text to an existing file:
txt >> catThe new line was added to the file:
This is the second line in test file #1.d while holding down Ctrl.Verify the test1.txt file’s content:
feline test1.txtThe cat command’s features can be mixed. To merge the output of two files and save the result in a new file, for instance:
cat test1.txt, test2.txt, and finally test4.txtfeline test4.txtYou can also add additional files to the end of an existing file as an alternative:
cat test2.txt test1.txt >> test4.txtfeline test4.txtIt should be noted that the files are added to the destination file in the order indicated.
If you use cat on a particularly large file, you’ll get a long, difficult-to-read string of data as a result. In order to divide it into pages, use | more:
cat test4.txt | moreThis only shows the first page of the file. The next page will scroll when you touch a key.
Use | less if you’d want to be able to move the display forward and backward.
c a t e r t. t l e sLine numbers in the output may be valuable to you, particularly for big files. Add the -n option to the cat command in order to enable line numbering:
txt, cat -nThe output should resemble the illustration.
With the command $, you can tell the cat to bold the end of each line and the spaces in between them.
Use the command to do so:
txt, cat -eThe output displays one $ at the end of the sample file test1.txt because there is only one line in it.
With the cat command, you may choose to see both the text between the tabs and the contents of the file.
In order to display tab-separated lines for a test run:
txt, cat -tI stand in for the tab space within the text.
When using the cat’s -s option, blank lines are not included in the output:
txt, cat -tList every CAT commandUse the —help command if you have difficulties remembering the options:
aid for catsNow that you know how to use the cat command in Linux, you should be able to utilize it effectively.
Interested in learning How to Use Linux Cat Command? See our list of the Top Linux Commands Every User Should Know.
Read more: