Cat Command in Linux with Examples

Expert-led Online Courses: Elevate Your Skills, Get ready for Future - Enroll Now!

In this article, you will learn what the Linux cat command is. You will also know why it is used, and the different options used with it. In the end, we will also be going through some of the practical examples of the cat command.

What is cat command in Linux?

The cat command is an abbreviation of concatenate. It is one of the most frequently used commands in Linux-based operating systems as it is highly efficient and improves workflow.

The cat command is a command-line-based utility that helps in many tasks like reading files, writing in files, viewing the content of files, concatenating files, redirecting output in the terminal and so many more things.

This command displays the contents of the files you want without having to open the file for editing.

Syntax of Linux cat command

Before we see the practical examples using the cat command, let us look at the general syntax of the cat command.

cat <options><file>

Now let us look at how we used this syntax to access the many amazing features of the cat command
Displaying the contents of a files

To display the contents of the file we simply use the syntax of the cat command with no option: cat <filename>

displaying the contents of a files

Displaying contents of multiple files

To display the contents of more than one file, simply add the filenames separated by space one after the other: cat <file1> <file2> <file 3> and so on.

displaying contents of multiple files

Creating a file with the cat command

If you want to create a file using cat straight in the terminal and not using the GUI, you can do it by using the syntax:

cat ><filename>

creating a file with cat command

Once you enter the command, the cursor will go to the next line and wait for your input. You can enter what you choose to write in the file you just created.

creating a file with the cat command

After you finish writing, you can press “ctrl” + “d” to save your work in the file you just created.

Displaying line numbers

If you want to display the line number of the content in a file, you can do so by using the option “-n” like this:

cat -n <filename>

displaying line numbers

Displaying “$” at the end of each sentence

If you want to display a dollar symbol at the end of each sentence, you do so do by using the flag “-e” like this :

cat -e <filename>

displaying dollar at the end of each sentences

The command “cat -e -n ubuntu.txt” will print the line number as well as a “$” at the end of each sentence as shown below:

displaying dollar at the end of each sentence

Displaying the tab-separated lines

To display or highlight the tab spaces in the file with “^I”, use the option “-T” like this: cat -T <filename>

displaying the tab separated lines

Displaying multiple files at once

Another way to show the contents of multiple files is to cat each of them individually such that they are separated by a semicolon. Use the following syntax: cat <file1>; cat <file2>; cat <file3> and so on

displaying multiple files at once

Using the redirecting operator

If you want to write the contents of a specific file to another file (new or existing), you can do so by using the redirection operator (>) in the syntax:

cat <old file> > <new file>

using the redirecting operator

If you enter the name of an already existing file and it already had contents in it, cat will override them with no warning, so choose the file name carefully

Appending with redirecting operator

If you want to append the content of 1 file to the contents of another existing file, you can do so by using the “>>” symbol like this:

cat <file 1> >> <file2>

This command will append the contents of file one to file 2

appending with redirecting operator

Redirecting multiple files

If you want to combine more than 1 file together in a single new file, you can do it by using the redirecting operator like this:

cat <file 1> <file 2> <file 3> <file 4> > <new file>

This command will copy all the files (file 1, file 2, file 3, and file4) into the new file.

redirecting multiple files

Writing in an already existing file

If you want to add some new lines or write in an already existing file, you can do so by using the syntax:

cat >> <filename>

writing in an already existing file

Once you finish writing the text and wnat to save and exit, press “ctrl” + “D”

Displaying content in reverse order

To display the contents of a file in reverse order, use the syntax and example as below:

tac <filename>

displaying content in reverse order

Suppressing empty line

If you don’t want the empty lines in your file to be shown in your output, you can suppress them by using the “-s” option like this:

cat -s <file name>

Below is the example of file with many empty lines:

suppressing empty line

Below is the example of “cat -s ubuntu.txt” to suppress the empty lines in the file:

suppressing empty lines

Marking the end of the file

To mark the end of the file with “EOF” (End Of File), you can use the syntax:

cat > <filename> << EOF

Removing blank lines

To completely remove and not suppress the blank lines in your file, you can use the “-b” option like this:

cat -b <file name>

Managing large content

If the content of the file is really big and it is not fitting in the terminal, you can manage it by using “| more” like this:

cat <filname> | more

Similarly, if your document is too small then you can use “| less” like this:

Cat <filename> | less

Opening a dashed file

If you want to open dashed files in the linux terminal, you can use the cat command followed by 2 hyphens “–” and the name of the dashfile you want to open. Use the following syntax:

cat -- “-dashfile”

This command will display the contents of the dashfile you specified.

A brief summary of the options used with the cat command

We have already look at most of the options in the above examples, nonetheless, let us look at all of them in brief.

1. -A

This option is exactly the same as -vET. Instead of using all the 3 options, you could just use “-A”

2. -b

This option numbers non-empty output lines. In other words, it gets rid or neglects the empty lines in the file
3. e

This option prints “$” at the end of eacj line.

4. -n

This option numbers all the lines of the file.

5. -s

This option supresses repeated empty output lines

6. -T

This option displays TAB charectars as ^I

7. -t

This option is eqvivalengt to “-vT”

8. -v

This option uses ^ and M- notation, except for LFD and TAB.

9. –help

This option shows all the information about the cat command like the syntax, options and a few examples

10. –version

This option shows the version information of the cat tool you are using.

Summary

As you have seen, the cat command is a really simple yet hoghly efficient tool that helsp in various tasks like reading files, writing in files, viewing the content of files, concatenating files, redirecting output in the terminal and so many more things.

You have learned what cat command is, the syntax it is used in, its option and also many practical examples of the cat command.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

DataFlair Team

DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today’s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.

Leave a Reply

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