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.
- Linux-running computer
- access to a command line or terminal window
Command Syntax for Cat
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 -n
Enter 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.
Examples of How to Use Linux Cat Command?
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.
1: Making a New File
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, cat
2: 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.
- To exit the prompt and write the changes to the file, hold the Ctrl key and press d.
Use the cat command to generate a new file.
4: Carry out step 4 again to produce test2.txt. Run:
txt, cat
5: Type:
Test file #2 is this one.
6: Hit Ctrl+d.
2: Show the Contents of One File
Run the following command to use cat to display the contents of test1.txt:
feline test1.txt
The content appears in the output as shown in the image below.
3: Show Contents of Several Files
Run the following command to see what both files contain:
txt, test1.txt, cat
4: Redirect a Single File’s Contents
A cat can store the contents of a file in a file as opposed to showing them on the screen.
txt > test3.txt in a cat
The 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.txt
The result shows:
- If a file is exported that already exists, the contents will be overwritten:
txt > test3.txt in a cat
fel test3.txt
The following text is now present in the test3.txt file:
5: Redirect Multiple Files’ Contents
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.txt
The screenshot below shows how the output displays the contents of both files.
6: Show the Contents in the Opposite Direction
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 tac
7: Attach the contents of a file to another file
The 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.txt
the test3 file by executing:
fel test3.txt
Test 3’s material should appear first, followed by Test 1’s content.
8: Add Text to a Current File
A similar command can be used to add text to an existing file:
txt >> cat
The 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.txt
9: Combinational Operations
The 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.txt
feline test4.txt
You can also add additional files to the end of an existing file as an alternative:
cat test2.txt test1.txt >> test4.txt
feline test4.txt
It should be noted that the files are added to the destination file in the order indicated.
10: More and Fewer Choices (Control Big Files)
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 | more
This 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 s
11: Display Line Numbers
Line 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 -n
The output should resemble the illustration.
12: Display the Line’s End
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 -e
The output displays one $ at the end of the sample file test1.txt because there is only one line in it.
13: Display TAB Separated Lines
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 -t
I stand in for the tab space within the text.
14: Eliminate White Space
When using the cat’s -s option, blank lines are not included in the output:
txt, cat -t
List every CAT command
Use the —help command if you have difficulties remembering the options:
aid for cats
Conclusion
Now 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:
- Linux Kernel 5.7 Released with New Features and Improvements
- How to Show Hidden Files in Linux Command ?