How to Use the $cat Command in Linux

Wondering how to use the $cat command in Linux?

Some of the most common tasks a user does on their computer are the functions performed on different types of files. No matter why and where you use your computers, you probably have and are fiddling around with the files on your computer. Copying, moving, editing, reading, etc., are commonly associated with files and folders. 

Depending on the operating system, users choose to carry out said tasks either through the GUI or by typing executable commands on the CLI. Either way, the same tasks are performed. It just depends on the user’s preference. 

However, users of Linux and its distros lean more towards using the command terminal and the executable commands to perform their tasks. 

In this article, we will be discussing how to use the $cat command and its utility when it comes to computer files. 

So, let’s get started!

The cat Command.

$cat stands for “concatenate” and is one of the most commonly and frequently used commands in Linux and its distros. It can be used to perform multiple functions on files, such as creating a file, viewing the contents of a file, etc.

The general syntax of the $cat command is.

$ cat operator filename

Next, we will be looking at how we can use different operators to carry out such operations on our files. 

Create a File Using cat Command.

First,  we will discuss how to create a new file using the $cat command. Creating files with $cat is straightforward. Just follow these steps to learn how to do it.

  1. Open the Terminal.
  2. Once opened, type in the following command to create a file:
$ cat > new filename
  1. Here we create a new text file with the name newfile.txt using this command: 
$ cat > newfile.txt
cat Command in Linux

This is how you can create a new file in Linux using the $cat command. 

View a File Using cat Command.

As mentioned before, you can use the $cat command to view the contents of a file without opening it. The contents will be displayed as output in the terminal window. For this, you can use $cat with the following syntax.

$ cat filename

For example, we open the file “doc1” on our Terminal.

cat Command in Linux

You can also use $cat to open multiple files simultaneously. For this, just repeat the filenames one after the other in this way:

$ cat filename1 filename2 filename3…..

You can also view the contents of a file so that every output line is preceded by a number. This can be done by using $cat in the following way:

$ cat  -n filename
filename

For this specific output, we use the “-n” operator. You should see an output like this. 

You could also hide some private files or folders on your Linux system to make them more secure.

Copy Contents of One File to Another.

You can come across multiple situations where you have to copy the things present in a file and paste them into another file. Thankfully, this task can be easily completed using the $cat command as well. 

  1. Open the Command Terminal
  2. Once opened, use the following command as a template to copy-paste the contents on your computer. 

$ cat name of the file which is being copied > name of the destination file

  1. For example:
$ cat doc1 > doc
cat Command in Linux
result

If you check the content of your destination file, you should see that the contents of the first file have been successfully copied. 

Append One File at the End of Another.

Appending the contents of a file is pretty similar to copying the contents of one file to another. The difference is that in appending a file, the contents of the first file are pasted at the end of the destination file. If you want to append a file to another, you can use the following command.

$ cat name of the file being appended >> destination filename

Using More and Less With $Cat.

There are times when the text written in a file is extensive and it can’t fit in the Terminal’s screen. When we use the $cat command to view the contents of a file, everything gets loaded on the screen in an instant and you can’t read whatever is written. 

To help you navigate through the content, there is the provision of “more” and “less” operators. When we use “more” in tandem with $cat, everything is loaded onto the screen, and then you can navigate up and down using the move operator. 

When you use the “less” operator, only the information which can fit in a Terminal’s screen is loaded. Further information is uploaded simultaneously as you move down. 

You can use both “more” and “less” in the following way.

$ cat filename | more or less


Add Text to an Existing File.

You can also add text to an existing file using the $cat command. This can be done by the following steps.

Open the Terminal.

Once opened, first use the following as a template to add text to the file of your choice. 

$ cat >> filename
cat Command in Linux
cat Command in Linux

When you press enter after typing the above-mentioned command, you are required to type the text you want to be added to the file and then press enter to save it. 

This was a guide on how to use the cat command in Linux and its distros. We looked at how you can use $cat to perform various regular commands on your files. $ cat is a great utility to have and we hope that this guide helped you in understanding the power of the $cat command.  If you have any further queries, please let us know in the comments. 

If this guide helped you, please share it. 🙂

Leave a Reply

Your email address will not be published.

Previous Post
Contacts App Not Working on Mac

How to Fix Contacts App Not Working on Mac

Next Post

How to Use the chmod Command in Linux?

Related Posts