Connect with us

Website Tutorials

How to Use the Cat Linux Command

Published

on

How to Use the Cat Linux Command

“Cat” is one of the most well-known commands in Linux, and has a history dating back to the 1960s and 70s. The original purpose of the command was to concatenate files and display the results on the standard output, from which functionality the command gets its name – short for “concatenate”. Throughout all the many changes that Linux has experienced since then, cat has remained a simple tool, even with all the under-the-hood changes.

In this tutorial, I’ll show you the various use cases of cat, showcasing its versatility.

The Basic Use of Cat

The most common use of cat, even for novice Linux users, is its ability to quickly display the contents of a file without needing to open it in a separate text editor. Unlike an editor like “vi”, which has a separate editing interface and a complete set of commands to learn, you just run the cat command, and the output is shown directly below in the same bash terminal. For example, here’s how to quickly use the cat command to display the contents of a file in Linux:

cat testbackup.txt

Here’s what it looks like:

Displaying the Contents of a File Using Cat
Displaying the Contents of a File Using Cat

As you can see, it quickly shows the file text without any fuss. Particularly for smaller text files without any scrolling, like the one above, it’s incredibly useful. But if your file has a lot of text, it’ll simply scroll past you without giving you a chance to read what it printed to the terminal. To deal with this, you can simply use the “more” command along with cat, which allows you to use the spacebar to scroll through the content.

Here’s what the command looks like:

Advertisement
cat test.txt | more

And here’s the output:

Cat with More
Cat with More

You can see that the interface stops when the page is full, and you can click the spacebar to display the next round of text. You can use this useful trick to regulate the display of text for any Linux command, not just cat. Any time you have to print a large amount of text on the terminal and want to read it page by page, pipe the result to “more” as shown above and you’re set!

Using Line Numbers for Easier Readability

Sometimes a text file is too large to easily scan through, and you want to refer to a specific piece of code or a command in a script. In such situations, it’s easier to provide the line number, instead of the text in the file. Or you might be trying to debug some code, and you know that the error occurs on a certain line number. Cat can help you add line numbers to the contents of the file that it displays using the following command:

cat -n filename.txt

Here’s the output:

Line Numbers in Cat
Line Numbers in Cat

You can see above in the text file standard “Lorem Ipsum” content, that it shows all the places where a newline character exists. It’s important to note that line numbers do not represent the breaks in the terminal window when the text wraps around to the next line, as this will differ depending on the size of the viewport. Instead, line numbers are standard location references that won’t change from system to system.

So if you have to refer to or find a line number for a coding error, just use “cat” with the “-n” flag.

Using Cat to Merge Files

As I mentioned earlier, the original use of cat was to concatenate files, not just display them. Merging files is as easy as displaying them – simply list the file names side by side like this:

Advertisement
cat testbackup.txt example.txt

The above command will take the two files “testbackup.txt” and “example.txt”, and display them one after the other on the terminal like this:

Concatenate and Print
Concatenate and Print

You can see that cat takes the two files and just shows them consecutively. By itself, this is hardly useful. But the cool feature of cat is that you can redirect the output to any file of your choice. So, for example, the following command will merge the two files into a new third file called “mergedfile.txt”:

cat testbackup.txt example.txt > mergedfile.txt

Cat won’t show you anything when you execute the above command, because the output is no longer directed towards the screen, but into the new file. If the file doesn’t already exist, it will be created.

You can repeat the above procedure for any number of files that you want to concatenate – you’re not limited to just two. This was the basic, original use-case scenario for cat.

Appending to Text Files Using Cat

A cool use for cat is appending text. For example, if I want to append text to a file called “example.txt”, I use the following command:

cat >> example.txt

This will give you a new line, where you can manually type or paste the additional content you want to append to the file. When you’re done, press “Ctrt+d”. Then you can display the results using the cat command as before. Here’s what it looks like:

Advertisement
Appending Using Cat
Appending Using Cat

In the above screenshot, I’ve appended the line “This is additional stuff” to the file “example.txt”. Easy, right?

Creating a New File

You can also use the cat command on Linux to create a new file. Like other commands such as “touch”, merely specifying a file that doesn’t exist causes it to be created. For example, to create a file called “newfile.txt”, simply type the command:

cat > newfile.txt

However, cat does more than simply create a new file. You can also type the content that you want in the lines that follow. When you’re done, press “Ctrl+d” as before, and the file is created with your content.

If you just want to create a new file without any content and don’t want to add anything to it, it’s easier to use the “touch” command instead like this:

touch example.txt

These two methods of creating a new file are better alternatives than opening a dedicated text editor like vi, which opens a new interface, especially if the content you want to write in the new text file is just a few lines, for testing purposes, for example.

Combining with Grep for Finding Strings

Since cat sends its output to the standard output, we can easily combine it with other commands like “grep”. For example, let’s say I want to find the text “Lorem” in the file with boilerplate text. I can do so by chaining the two commands together like this:

Advertisement
cat test.txt | grep "Lorem"

This will display the lines in the file where the word “Lorem” appears, and color the matched string so that it’s easy to see. The pipe (|) is one of the most useful tools that Linux admins use all the time. Here’s a tutorial on how to maximize its effectiveness of the pipe command.

If you want to learn more about grep, here’s an article on how to use it with multiple strings at the same time.

Conclusion

You can see that cat is a versatile command in Linux for displaying, merging, and searching text files. For a program that’s so old, it’s held up very well. Hopefully, this tutorial gave you an easy introduction to one of the most venerable Linux commands!

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.