99.9% of Linux users use the cat command every day.
Despite this, most people do not take advantage of its full potential.
In this thread, I'll explain what the cat command is and show you some options you probably didn't know about (but you should):
Despite this, most people do not take advantage of its full potential.
In this thread, I'll explain what the cat command is and show you some options you probably didn't know about (but you should):
The cat command is one of the most commonly used in Linux. The cat command gets its name from its ability to concatenate files. It has the ability to concatenate files, read and write file contents to standard output.
If you do not specify a filename to read from, or if the filename is replaced with a single hyphen (-), the data will be read from standard input (stdin)
Before we get into how to use the cat command, let's go over the basic syntax.
The syntax for the cat command is as follows:
$ cat [OPTION] [FILE]
The syntax for the cat command is as follows:
$ cat [OPTION] [FILE]
- OPTION - to view the available options that are available use the --help (cat --help) option or its man page (man cat).
- FILE - 0 or more files to concatenate or read data from.
- FILE - 0 or more files to concatenate or read data from.
Now that you know what the cat command is and how its syntax, let's look at how it's used and some of the options it has.
2. Print line numbers
The cat command has excellent option (-n) you can use to check the count of lines on a file . If you ever had a large text document and needed to add lines to it, this option is your best friend.
The cat command has excellent option (-n) you can use to check the count of lines on a file . If you ever had a large text document and needed to add lines to it, this option is your best friend.
You can then save the output to a file if you wish to, by just using output redirections operators (> and >>).
$ cat -s distros.txt > nolines.txt
The > operator will create a file (nolines.txt) if it doesn’t exist. Otherwise, it will overwrite the file.
$ cat -s distros.txt > nolines.txt
The > operator will create a file (nolines.txt) if it doesn’t exist. Otherwise, it will overwrite the file.
If you wish not to overwrite the file use >> operator to append the contents to the end of nolines.txt file
$ cat -s distros.txt >> nolines.txt
$ cat -s distros.txt >> nolines.txt
4. Concatenating multiple files
With cat command you can also read (concatenate) contents of multiple files and display it to the standard output.
To do so you just separate the file names with spaces.
With cat command you can also read (concatenate) contents of multiple files and display it to the standard output.
To do so you just separate the file names with spaces.
This becomes very handy if you want to combine the contents of different files into one file.
Just like the touch command but cat allows you to have the ability to write text you want to be saved before actually creating a file. You can omit the text if you want to create an empty file.
If the file data.txt exists, it will be overwritten. To append the text to an existing file, use the '>>' operator.
$ cat >> data.txt
This is useful if you want to write to a file without having to open a full-fledged text editor.
$ cat >> data.txt
This is useful if you want to write to a file without having to open a full-fledged text editor.
From the output, the $ character represents the line endings.
That's it!
Did you learn anything new from this thread? If so, please let us know by replying in the comments.
If you're new here, do toss us a follow us (@linuxopsys) for more threads, tips and resources on Linux.
That's it!
Did you learn anything new from this thread? If so, please let us know by replying in the comments.
If you're new here, do toss us a follow us (@linuxopsys) for more threads, tips and resources on Linux.
Loading suggestions...