Using the cat command in Linux

The cat command is used to print the contents of text file files.

At least, that’s what most Linux users use and there’s nothing wrong with that.

Cat actually stands for “concatenate” and was created for Merge text files. But with one argument, it prints the contents of the file. Because of this, it is the go-to option for users to read files in the device without any additional options.

Using the cat command in Linux

To use the cat command, you must follow the command’s specific syntax:

cat [options] Filename(s)


  • [options] Used to modify the default behavior of the cat command such as using -n Option to get numbers per line.
  • Filename This is where you will enter the name of the file you want to work with.

To make things easier, I’m going to use a text file named Haruki.txt In this directory which contains the following lines of text:

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 would the output be when using it without any options? Well, let’s take a look:

cat Haruki.txt
Use the cat command on Linux

As you can see, she printed out the entire text file!

But you can do much more than that. Let me show you some practical examples.

1. Create new files

Most Linux users use the touch command for Create new files But the same can be done with the cat command, too!

The cat command has one advantage over the touch command in this case, in that you can add text to the file during creation. looks good. is not it?

To do this, you must use the cat command by appending the file name with the > As shown:

cat > Filename

For example, here I created a file named NewFile.txt:

cat > NewFile.txt

Once you do that, there will be a blinking cursor asking you to type something and eventually, you can use it Ctrl + d to save the changes.

If you want to create an empty file, just press Ctrl + d without making any changes.

using cat command

That’s it! Now, you can use the ls command to unhide a file Contents of the current working directory:

Use the ls command to list the contents of the current working directory

2. Copy the contents of the file to a different file

Think of a scenario in which you want to forward file content file to file b

Sure, you can copy and paste. But what if there are hundreds or thousands of lines?

basic. You can use the cat command with data stream redirection. To do this, you must follow the given command syntax:

cat FileA > FileB


If you use the above syntax to redirect the contents of the file, it will clear the contents of the file from FileB and then redirect the contents of the file.

For example, I’ll use two text files FileA and FileB that contain the following:

Check the contents of the file with the cat command

Now, if I use redirection from FileA to FileB, then FileB data will be removed and then FileA data redirected:

cat FileA > FileB
Redirect the content of the file with the cat command

Similarly, you can do the same thing with multiple files:

cat FileA FileB > FileC
Redirect the content of multiple files using the cat command

As you can see, the above command removed FileC data and then redirected FileA and FileB data.

Append the content of one file to another

There are times when you want to append data to existing data and in that case, you will have to use a >> rather than being single >:

cat FileA >> FileB

For example, here I’m going to redirect two files FileA And FileB to FileC:

cat FileA.txt FileB.txt >> FileC.txt
Redirect the content of the file without overriding it using the cat command

As you can see, it has kept file data FileC.txt The data is appended at the end.


You can use the >> To add new lines to an existing file. Uses cat >> filename And start adding the text you want and finally save the changes with it Ctrl+D.

4. Show line numbers

You may encounter such scenarios where you want to see the number of lines, and this can be achieved using -n option:

cat -n File

For example, here, I used a file -n option with Haruki.txt:

Get the number of lines in the cat command

5. Remove the blank lines

Did you leave several blank lines in your text document? Cat command will fix it for you!

To do this, all you need to do is use a file -s science.

But there is a downside to using it -s science. You still have one empty space:

Remove blank lines with the cat command

As you can see, it works but the results are close to expectations.

So how are you going to remove all the blank lines? By plugging it into the grep command:

cat File | grep -v '^$'

here , -v Flag will filter results by the specified style and '^$' A regular expression that matches blank lines.

Here are the results when I used them on a scale Haruki.txt:

cat Haruki.txt | grep -v '^$'
Remove all blank lines in text files using the cat command with the grep regular expression

Once you have the perfect output, you can redirect it to a file to save the output:

cat Haruki.txt | grep -v '^$' > File
Save the output of the cat command by redirecting

This is what I’ve learned so far

Here’s a quick summary of what I explained in this tutorial:

order a description
cat <Filename> Prints the content of the file to the terminal.
cat >File Create a new file.
cat FileA > FileB file contents FileB will be overridden by FileA.
cat FileA >> FileB file contents FileA It will be appended at the end FileB.
cat -n File Shows the number of lines while deleting the file contents from the file.
cat File | more Pass the cat command to the more command to handle large files. Remember, it won’t let you scroll up!
cat File | less pass the cat command to the lower command, which is similar to the above, but allows you to scroll in both directions.
cat File | grep -v '^$' Removes all blank lines from the file.

It’s time to do sports

If you learn something new, implementing it with different capabilities is the best way to remember.

To that end, here are some simple exercises you can do with the cat command. They will be very basic as a cat, too One of the simplest commands.

For training purposes, you can Use our text files from GitHub.

  1. How do you create an empty file with the cat command?
  2. Redirect the output from the cat command to a new file IF.txt
  3. Can you redirect three or more file entries into one file? If yes, then how?

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *