If you have ever used Linux, then you are probably already familiar with cat commands in Linux. Cat commands are pretty useful, and they are very popular and versatile command in the Linux ecosystem. And are used very often.
There are four common usages of the cat command that you would get to see in a Linux ecosystem. You can use it for displaying a file, combining multiple files, echo text, or you can use it to create a new file.
But not all of us are well aware of different cat commands in Linux. If the same goes for you, too, then We are here to help you out.
We will be listing out some of the common cat commands in Linux that you can use. So here we go:
1. To view a single file
If you want to view a single file using the cat command, then you will need to use the below command in the terminal:
Command:
$cat filename
Output
It will show the content of the chosen file.
2. To view multiple files
To view multiple files at once in the terminal, you can run the following cat command:
$cat file1 file2
This will show the content of file1 and file2.
3. To view the contents of a file preceding with line numbers
You can also use cat commands in Linux to view contents for a file preceding with line numbers. For this, you have to use the following command:
$cat -n filename
It will show content with line number
example:-cat-n commands.txt
1)This is geeks
2)A unique array
4. Create a file
As mentioned earlier, you can use cat commands to create a file in Linux. For this, you have to use this simple command in the terminal:
$ cat >newfile
It will create a new file named newfile.
5. Copy the contents of one file to another file
You can also use cat commands to copy the contents of one file to another file. For this, run the below command:
$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]
The content will be copied in the destination file.
6. Cat command can suppress repeated empty lines in output
With the help of cat commands in Linux, you can easily suppress repeated empty lines in Output. For this, run the below command:
$cat -s geeks.txt
It will suppress repeated empty lines in Output.
7. Cat command can append the contents of one file to the end of another file
Cat commands are also helpful when it comes to appending the contents of one file to the end of another file.
$cat file1 >> file2
Will append the contents of one file to the end of another file.
8. Cat command can display content in reverse order using tac command
You can also display content in reverse using the tac command. For this, run the below command:
$tac filename
Will display content in reverse order
9. Cat command can highlight the end of line
If you wish to highlight the last line of your file, you can easily do so by using a simple CAT command. For this, run the below command:
$cat -E "filename"
Will highlight the end of line.
10. Instead of writing -vET in the command run -A command-line option
If you wish to run the –v, -E and the –T option together, then you can ditch writing the –vET command completely. Instead, you can simply run the –A command. For example:
Command
$cat -A "filename"
So those were some of the basic cat commands in Linux that you should be aware of. Now it is your call to go ahead and check these commands out and see how they are working for you. Also, for any other questions, do comment below.
Your email address will not be published. Required fields are marked *
Comment
Your name
Email address
Save my name, email, and website in this browser for the next time I comment.