en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com

How to Use the CAT Command in Linux + Examples

In this article, we are going to explain how to use the CAT Command in Linux as well as show some examples of how it can be used.

Linux Tutorial Apr 08, 21 by Nisal N 6 min Read
How to Use the CAT Command in Linux + Examples

Short for the word "concatenate," the cat command is one that is most commonly and frequently used in Linux operating systems. 

The cat command makes it possible for you to create multiple or just single files while viewing the contents of them. Cat commands will also show those specific cat files and will redirect the output in the files or terminal. 

By taking advantage of the various cat commands, you'll be given more control of what you can do with Linux, making the experience not only a richer one but is also more convenient and simple as well. 

In this article, you'll be shown how to use cat commands as well as some examples of just what they can do.

General Syntax

Create a File Using cat

In order to generate a file using the cat command, simply enter in cat > filename. Once you have created a file this way, the cursor will move to a new line, and it is on this line that you can begin typing. 

Through this technique, you’re given an easier way to begin a text file, and when you're ready to edit it, simply press Ctrl+D to do so. The file will save with whatever it is you used for the filename element.

Another way to create files is by using the Nano command, check it out. 

Displaying a File Using cat

With the cat command, you can show a file on the screen as well. In order to do this, all you have to do is take out the ‘’greater than’’ symbol. 

Then, you can look at the file page by individual image using the more command or use the less command to view less of it.

Concatenate

As the entire point of CAT, commands is concatenation, you should be able to perform this task. You can concatenate several different files to the screen using the command cat with the name of your file directly following it. Then, add the second file you want to add and follow it with <newfile> to execute the command.

Show the Files in a Reverse Order

In order to show a file in reverse order, simply use the command tac before the name of your file. By doing so, you technically aren't using the cat command. It is technically the tac command as it is labelled, but it does the same thing in reverse.

Showing Line Numbers

If there are non-empty lines in a file, you can use the -b command and then file name to number them. If there are lines that possess no characters whatsoever, they won't get a number. In order to get the lines independent of whether they are blank or not, type cat -n before the file name.

Display the Ends of Each Line

Oftentimes when you are parsing data files, a programmer will see that there is a problem because of the hidden characters tacked onto the ends of a line, something they likely weren't expecting. This includes invisible characters such spaces. This sort of error will prevent them from showing their work correctly. In order to show the dollar as the end of a line character to stop this from happening, enter cat -e before the file name.

Reduce the Number of Blank Lines

Whenever you show the information within a file using the cat command, it is likely that you don't want to see it when there are many blank lines outlined consecutively. In order to lessen them and condense them down into one single blank line, type cat -s before the name of your file.

Show Tabs

When you are displaying a file that is currently using tab delimiters, you won't often see the tabs themselves. The command ^I will be shown instead of the tab, which makes it easier for you to actually see them.

Don't forget to check our article on how to use the echo command in Linux! Read more

Redirecting Many Files Contained in One Single File

Doing so will create a file, and any new output will automatically be redirected into a brand new file.

Sorting Content of Many Files Into a Single File

This will create a new file and the output of the cat command will be piped in in order to sort them out, and the result will be redirected into a brand new file.

Look at Content of Multiple Files in the Terminal

By using # cat, you can look at each content of the files in the system.

Put Many Lines in a Space

If you use the -e option and it shows $ at the end of a string of text, it will eliminate any gap that is found between the paragraphs, which is a useful option for squeezing multiple lines into one single line.

PS: check out our Linux Grep command

Show Many Files at One Time

You can view the content of each of the files with a semicolon in order to do so.

Use the Standard Output With a Redirection Operator

You can easily redirect the standard output of a file and move it into a new file using the greater than symbol, which is the > (symbol).

Redirecting Standard input With the Redirection Operator

If you use the standard input less than symbol < to redirect it, the output file name of the input name will be shown to you in the terminal.

Appending Standard Output Using Redirection Operator

Append the item in the existing file using two greater than symbols, or >> — by doing so, the contents of the file will be appended to the end of the other file.

Conclusion

The cat command is one that is commonly used with Linux operating systems. In reality, it is arguably the most frequently used command. Knowing the basic commands will allow you to explore those commands and make the most out of using them. 

The man page of your cat command will give you even more options to work with, letting you use even more command prompts.

In this article, we've looked at the different commands you can use with the cat function so you can get a clearer idea of what exactly you can do with the Linux terminals.

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment