Using cat Command in Linux
The cat command is used to print the file contents of text files.
At least, that’s what most Linux users use it for and there is nothing wrong with it.
Cat actually stands for ‘concatenate’ and was created to merge text files. But withsingle argument, it prints the file contents. And for that reason, it is a go-to choice for users to read files in the terminal without any additional options.
Using the cat command in Linux
To use the cat command, you’d have to follow the given command syntax:
cat [options] Filename(s)
Here,
[options]
are used to modify the default behavior of the cat command such as using the-n
option to get numbers for each line.Filename
is where you’ll enter the filename of the file that you want to work with.
To make things easy, I will be using a text file named Haruki.txt
throughout this guide which contains the following text lines:
Hear the Wind Sing (1979)
Pinball, 1973 (1980)
A Wild Sheep Chase (1982)
Hard-Boiled Wonderland and the End of the World (1985)
Norwegian Wood (1987)
Dance Dance Dance (1990)
South of the Border, West of the Sun (1992)
The Wind-Up Bird Chronicle (1994)
Sputnik Sweetheart (1999)
Kafka on the Shore (2002)
After Dark (2004)
1Q84 (2009-2010)
Colorless Tsukuru Tazaki and His Years of Pilgrimage (2013)
Men Without Women (2014)
Killing Commendatore (2017)
So, what will be the output when used without any options? Well, let’s have a look:
cat Haruki.txt
As you can see, it printed the whole text file!
But you can do a lot more than just this. Let me show you some practical examples.
1. Create new files
Most Linux users use the touch command to create new files but the same can be done using the cat command too!
The cat command has one advantage over the touch command in this case, as you can add text to the file while creating. Sounds cool. Isn’t it?
To do so, you’d have to use the cat command by appending the filename to the >
as shown:
cat > Filename
For example, here, I created a file named NewFile.txt
:
cat > NewFile.txt
Once you do that, there’ll be a blinking cursor asking you to write something and finally, you can use Ctrl + d
to save the changes.
If you wish to create an empty file, then just press the Ctrl + d
without making any changes.
That’s it! Now, you can use the ls command to show the contents of the current working directory:
2. Copy the file contents to a different file
Think of a scenario where you want to redirect the file content of FileA to the FileB
Sure, you can copy and paste. But what if there are hundreds or thousands of lines?
Simple. You use the cat command with the redirection of data flow. To do so, you’d have to follow the given command syntax:
cat FileA > FileB
🚧
If you use the above syntax to redirect file contents, it will erase the file contents of the FileB and then will redirect the file contents of the FileA.
For example, I will be using two text files FileA and FileB which contains the following:
And now, if I use the redirection from FileA to FileB, it will remove the data of FileB and then redirect the data of FileA:
cat FileA > FileB
Similarly, you can do the same with multiple files:
cat FileA FileB > FileC
As you can see, the above command removed the data of FileC and then redirected the data of FileA and FileB.
Append the content of one file to another
There are times when you want to append data to the existing data and in that case, you’ll have to use the >>
instead of single >
:
cat FileA >> FileB
For example, here, I will be redirecting two files FileA
and FileB
to the FileC
:
cat FileA.txt FileB.txt >> FileC.txt
As you can see, it preserved the data of the FileC.txt
and the data was appended at the end of it.
đź’ˇ
You can use the >>
to add new lines to an existing file. Use cat >> filename
and start adding the text you want and finally save the changes with Ctrl+D
.
4. Show the numbers of line
You may encounter such scenarios where you want to see the number of lines, and that can be achieved using the -n
option:
cat -n File
For example, here, I used the -n
option with the Haruki.txt
:
5. Remove the blank lines
Left multiple blank lines in your text document? The cat command will fix it for you!
To do so, all you have to do is use the -s
flag.
But there’s one downside of using the -s
flag. You’re still left with one blank space:
As you can see, it works but the results are close to the expectations.
So how would you remove all the empty lines? By piping it to the grep command:
cat File | grep -v '^$'
Here, the -v
flag will filter out the results as per the
specified pattern and '^$'
is a regular expression that matches the empty lines.
And here are the results when I used it over the Haruki.txt
:
cat Haruki.txt | grep -v '^$'
Once you have the perfect output, you can redirect it to a file to save the output:
cat Haruki.txt | grep -v '^$' > File
That’s what you’ve learned so far
Here’s a quick summary of what I explained in this tutorial:
Command | Description |
---|---|
cat <Filename> |
Prints the file content to the terminal. |
cat >File |
Create a new file. |
cat FileA > FileB |
File contents of the FileB will be overridden by the FileA . |
cat FileA >> FileB |
File contents of the FileA will be appended at the end of the FileB . |
cat -n File |
Shows the number of lines while omitting the file contents of the File. |
cat File | more |
Piping the cat command to the more command to deal with large files. Remember, it won’t let you scroll up! |
cat File | less |
Piping the cat command to the less command, which is similar to above, but it allows you to scroll both ways. |
cat File | grep -v '^$' |
Removes all the empty lines from the file. |
🏋️It’s time to exercise
If you learned something new, executing it with different possibilities is the best way to remember.
And for that purpose, here are some simple exercises you can do with the cat command. They will be super basic as cat too is one of the most basic commands.
For practice purposes, you can use our text files from GitHub.
- How would you create an empty file using the cat command?
- Redirect output produced by the cat command to a new file
IF.txt
- Can you redirect three or more file inputs to one file? If yes, then how?