Cat is the short form of Concatenate command, and if you are working on a Linux operating system, this is one of the frequently used commands. With the help of the cat command, a user gets to create single or multiple files, view the file, concatenate files, and also redirect the output of the files on the terminal to see the information without opening them. With the help of this article, we are going to show you the different methods to use the cat command and get the required results.
What Is A Linux Operating System? [Definition]
Before we start with the example of the cat command and its functions, let’s first put a little light on the Linux operating system. A lot of people that are not from technical backgrounds think that there are only two operating systems. The first one is Microsoft’s Windows, and the second is Mac OS which is only present in Apple computers and can’t be downloaded and used on other machines. But there is a third operating system as well, and it is one of the most powerful, versatile, and flexible operating systems of all three. Yes, we are talking about Linux.
Linux, in a number of ways, is similar to your Windows and macOS; just like other operating systems, it does have a graphical user interface, and there are some basic software programs which you can find across the operating systems such as photo editors, video editors, word processors and more. Thus, you can use any computing machine using Linux.
But Linux is quite different from other operating systems as well in a lot of ways. One of the first features which make Linux stand out from the group of operating systems is that it is open-source software. The code which was used to develop Linux is completely free, and it is present in the public repository for people to view and download. In addition to this, if you have the required skill, you can even contribute to its code and make some new changes to it.
Who Uses Linux?
Well, you are using it right now, but you don’t know about it; around 2/3rd of all the web pages on the internet are using Linux servers to fetch the data that you require. Companies tend to choose Linux for their servers because it is pretty secure and, at the same time, flexible to use. Even Android uses Linux in some way to provide you all the functionality that you need with the operating system of your smartphone.
Keep this in mind, Linux is free to use, and no one owns it. There are a number of distributions of Linux, which are also called distros; some companies provide distros for free, while some might ask for a license fee. But the most famous Linux distros come at zero cost, and they are Ubuntu, Kali Linux, Mint, Debian, etc.
CAT Command Syntax
To be able to use cat commands with proficiency, you need the syntax of the command and what each word does in the command. The primary purpose, as we already said earlier is to provide users with an essential file operation such as displaying the file and creating a new file as well.
The cat command syntax is written in the given form:
cat [options] [filename]
Options:- Each command in Linux comes with a number of options that can be used to perform different things in the operating system. To find out the options for cat command, you need to type in cat –help and press enter.
Filename:- A file name could be anything that you have stored in your system. When you are using the cat command, you need to type in the filename that you want to create or move.
How To Use Cat Command With Example
Now that you are done with the basics of the cat command syntax, let’s move on to the main part of the article, which is to show you the different operations of cat commands with examples for your better understanding.
1. Displaying File Content
Starting off with the most basic cat command, which is to display the contents of the file on the command line. Let’s take an example to see how it works; let’s say you want to display the content of file /etc/examplefile1 on your terminal. In order to do so, you need to type in the following command:
cat /etc/examplefile1
2. Redirect Content Of File
In order to redirect the output of the file using stdout on your screen, you can use the cat command and display the same information on the file. Or in other words, if you want to copy the contents of examplefile1 to examplefile2, you need to use the following command with (>) operator.
cat examplefile1.txt > examplefile2.txt
In a lot of cases, you will be using the regular copy command to get this done, but this is another use of the cat command which might be asked in an interview. On the other hand, if the file is not present in your system, the cat command will automatically create a file with the same name, which is examplefile2.txt. But if the file is already present, then the contents of the file will be overwritten.
In case you want to append something from the examplefile1.txt to examplefile2.txt without overwriting the text which is already present in the second file, then you need to use the (>>) operator.
cat examplefile1.txt >> examplefile2.txt
3. Printing The Line Numbers
If you are someone who types a lot of codes, you can use this option to find out the content for the specific lines in your code document or even in your text file. For this operation to work, you need to use the -n option of the cat command:
cat -n /etc/examplefile1.txt
4. Suppress Repeated Empty Lines
In order to omit the repeated empty lines in your output, you can use the -s option of the cat command. Type in the following command to get the required results:
cat -s examplefile1.txt
5. Show TAB Characters
Sometimes you want to see the difference between the tabs and the spaces, so you can find that by using the -T option of the cat command. You need to write the following command on the terminal for finding the TAB characters:
cat -T /etc/examplefile.txt
6. Creating File With Cat Command
With this example, we are going to show you how to create a file by the custom name using the cat command. Let’s suppose we want to make a file with the name examplefile3 using the cat command then we need to write the following things on the command line:
Cat >examplefile3
Now the terminal will wait for your response, and here you need to type in the text that you want to be stored in the examplefile3. Once you are done writing the text, press the buttons ctrl+D. This will take you out from making the edits in the file. Now you can see the content of the file on the terminal without actually opening it separately.
7. Using CAT Command With More & Less Options
There is no doubt that you might have files on your computer that are quite big enough, and the content present in them can’t be displayed at the terminal all at once. But if you want to display the contents of such a big file, you can show them on the terminal in parts using more and fewer options of the cat of the cat command.
If you don’t use the more and less option, the file will be displayed up to the point where the command line and display the text present in the file. After that, you can click on enter to scroll down and see the rest of the file. In addition to this, you can use the spacebar to scroll down the whole page.
At the end of the terminal, you will see a percentage such as more – (50%); this means the text you are currently seeing is 50% of the total file. As you scroll down, this percentage will increase.
To write down the more and less command with a cat, you need to type in the following command:
cat bigfileexample | more
cat bigfileexample | less
You can also use these two independently with just the file name and no addition of cat, and the results will be the same.
8. Viewing All Files
In order to view all the files present in the given directory, you need to use the following option of cat:
cat *
Also, if you are looking to access the files with a specific file type, you need to use the same option of the cat command but provide the file type for which you want to display results. For example, if we want to see the files that have .txt file format, then we will use the following command:
cat *.txt
9. Printing Files Using cat
This might be a lesser-known command option for cat, but one can actually print the files using the cat command. In this example, we want to print the file named printfile.txt, which is present on your computer.
cat printfile.txt > /dev/lp
Note:- In some new systems, the dev/lp does not work, so you need to use another command which takes in the help of lpr to provide you the printing option.
cat printfile.txt | lpr
10. Joining The Binary Files
With the cat command, one can even concatenate the binary files. In the early days of the internet, when having a broadband connection to access the web was expensive, people used to download large files in sub-parts and then concatenate all them in one file when all of them were downloaded.
cat file1.bin file2.bin file3.bin > large.tar.gz
11. Testing Audio Devices
With the use of a cat, a user can send sound signals to the devices that are connected with their system. Also, with the cat command, you can record the audio from the connected equipment and play it on the other one.
cat audio1>/dev/dsp
cat recording1.au >/dev/audio
12. Linux Information
You can also output the information related to the Linux distribution which you are using on your system by using the following commands:
cat /proc/cpuinfo
cat /proc/ meminfo
cat /proc/version
13. Displaying Multiple Files At Once
Suppose you have three files and each of them has a resembling data, and you want to find out the difference in their content. Then you can use the cat command to display the content present in multiple files on the terminal at the same time. You need to use the following command:
cat examplefile1; cat examplefile2; catexamplefile3
14. Sorting Out Content
Find out all the files that you want to sort and add them in the cat command along with sort. Let’s take an example of examplefile1, examplefile2, examplefile3, and you need to sort these three files out and save the result in the result.txt file. In order to do so, you need to add the following command in your terminal:
cat examplefile1 examplefile2 xamplefile3 | sort > result
15. Piping Content Into Another Command
This is a bit tricky command to pull out, and in most cases, you might not even need it. But it is good to know that cat commands can also pipe operators when required. In this example, we are using grep command alongside cat to pipe operator. Now we want to send a specific part of the examplefile1.txt, which is “good day,” to examplefile2.txt.
cat examplefile1 examplefile2 | grep “good day”
From the above command, both the files will go into the grep command, and then grep will do its operation and will provide the display of a line that has the word “good day” in it.
Wrapping Up
These are the examples and use cases of cat commands in Linux. There are tons of more commands to perform the various operations of the cat command, but the cat is the most popular one that is well known in the community and easy to use. Hope this article helped you understand the importance and function of the cat command. Please let us know if you would like us to add a specific operation to the article in the comment section below.