The cat command in Linux is not as cute as a real cat, but it plays a key role in Linux.
The cat command allows you to view the contents of a text file from a Linux command line. In this guide, we’re going to talk about what the cat command is, how it works, and how you can use it to create, read, and modify files. Let’s get started!
Creating a File Using the Cat Command
We’re going to need a few files to read before we can use the cat command. Let’s create two files: one called
non_berries.txt and one called
We can create a file using the cat command like this:
cat > non_berries.txt cat > berries.txt
These commands create the files we need for our example.
The cat command prints out an empty response when used alone, and if we use the arrow sign, we can use it to populate a file with no contents. If you use these commands on a file that already exists, the file you have selected will be overwritten.
If you use two arrow signs (>>), cat will add text to the end of a file. We’ll discuss that later in this tutorial. These operators are called “redirection operators”. They transfer the contents of one file to another file, or the contents of a standard input to a standard output.
Now, let’s open up our files and add some text. In the
non_berries.txt file, we will add:
Open up the
berries.txt file and add the following text:
We now have two files that we can work with for the rest of this tutorial. To summarize, you can use the
cat > file.txt command to create a new file.
Reading Files Using Cat
Let’s take a look at the files we have created. To do so, we can use the cat command:
This command reads the contents of the
non_berries.txt file and prints them to the console:
We can get even fancier with the cat command. Suppose we want to see line numbers alongside our file. We could do so by adding the -n flag to our command:
cat -n non_berries.txt
The command displays the contents of the file alongside line numbers:
You can see that there are three lines in our list. This is useful if you’re working with a long file and you want to see how many lines of text are in that file. It’s also useful if you want to work with a specific line inside a file. You would need to know the number of the line you want to change before you amend that specific line.
Redirecting the Contents of a File
The cat command can combine files, or concatenate them. That’s how the cat command got its name: cat is short for concatenation. Pawsome!
Our non_berries.txt file contains a list of non-berry fruits; berries.txt contains a list of berries. We could print out both of these files to the console using this command:
cat non_berries.txt berries.txt
This command returns:
Our first file,
non_berries.txt, is printed first, followed by the second file. You can specify as many file names as you want when you are using the cat command.
We can use a similar syntax to concatenate, or combine, our files. Let’s say that we want to create a new file called fruits.txt with the items from both lists. We could do so by using the > operator or the > operator and the cat command:
cat non_berries.txt berries.txt > fruits.txt
If we open up our fruits.txt file, we will see a list of all the fruits from both our lists:
The > arrow creates a new file called fruits.txt and appends the contents of
berries.txt to the file. If we used the >> arrow, the contents of
berries.txt would be added to the end of any existing content in fruits.txt, if the file existed.
You can concatenate more than two files. It’s up to you. All you need to do is specify the names of the files you want to merge on the left hand side of the arrow, and the name of the file in which the merged contents should appear on the right side of the arrow (the destination file):
cat file1.txt file2.txt file3.txt > final_file.txt
Removing Empty Lines Using Cat
Do you have a file with a lot of empty lines which content you want to view? Cat has you covered. The cat command allows you to see a file without all the empty lines that are present.
Let’s change the contents of berries.txt:
There are two new lines in this file. If we use the cat command to print out the contents of the file, we’ll see those new lines. This is fine because it allows us to see exactly what is stored in our file. For practical purposes, we may want to remove new lines. That’s where the -s flag comes in handy:
cat -s berries.txt
The cat command displays:
The -s flag suppresses repeated empty lines in your text editor.
The cat command allows you to view the contents of a file from the Linux operating system command line. It can be used to view existing files, create a new file, or concatenate files.
Now you’re ready to start working with the cat command like a Linux professional!