10 Linux Cat command examples for beginners

"cat" command (short for concatenate) lets you display text file contents and a lot more! Read on for details.

As a Linux user, you need to know the usage of the best commands in the terminal to enhance your Linux experience. First of all, it’s good to know the basic commands used frequently.

One of these commands includes the “cat” command (short for concatenate). As a terminal user, the cat command lets you display text file contents and a lot more!

In today’s tutorial for beginners, we shall go through 10 Linux cat commands with examples.

Cat command usage in Linux with examples

Let’s get started.

1. File creation

One of the most common tasks that you can do with the “cat” command is to create a new file.

To do so, you need to use the following syntax.

$ cat > filename

This will instantly create a file named “filename,” and a cursor will start blinking. Now, you should be able to add content to your file.

In our example, we create a file name “learningcat.”

$ cat > learningcat

Creating file using cat command
Creating file using cat command

To return to your command line prompt, you need to press CTRL + D. It will quit and save the file.

2. Displaying file content

Cat command can also be used to view file content. All you need to do is run the cat command, followed by the filename.

$ cat filename

In our case, we can display the “learningcat” file that we just created.

$ cat learningcat

file display cat
Displaying file content using cat command

3. Display multiple file’s contents at once

The cat command also lets you show multiple file content at once.

$ cat filename1 filename2
$ cat learningcat helloworld!

multiple-file-content-display-once
Displaying multiple file’s contents at once

4. Append text to file

Cat command is not only useful for creating new files and displaying their content, but you can also use it to append text to the file. This means that the content will be added at the end of the file.

append-text-to-file
Appending text to file

5. Copying content from one file to another

Cat command is also useful when it comes to copying one file content to another. To do so, you need to use the following command.

$ cat filename1 > filename2

In the above command, filename1 content will be copied to filename2

$ cat learningcat > learningcatcopy

Check the screenshot below to get a better understanding.

Copying file content
Copying one file content to another

6. Displaying line numbers and total line number

You can also use the cat command to display line numbers. To do so, you need to use the following command.

$ cat -n filename

In our case, we created a file named, you guessed it, “longfile.”

Let’s see the command in action.

$ cat -n longfile

You can also display the total number of lines of a file.

display-line-numbers
Display line numbers

$ cat filename | wc -l

Now, let’s run it on the terminal.

$ cat longfile | wc -l

Total line number
Display total line numbers of a file

7. Display head and tail of the file

You can display the head and tail of the file by using the below commands

To display head, use the following syntax.

$ cat filename | head - number

The -number denotes the number of lines you want to show from the beginning of the file.

If you change “head” to tail, then it will showcase the last lines of the files. The syntax to do so is as below

$ cat filename | tail - number

Let’s see both in action.

head-and-tail
Show head and tail of the file

8. Insert $ at each line’s end

You can display $ at the end of each line using the following cat command

The syntax is as below.

$ cat -E filename
$ cat - E longfile

insert-$-at-end
Insert $ at the end of each line

9. View content in reverse order

If you want to see the file content in reverse, you can use the tac command. It is an exact reverse of the “cat” command.

$ tac filename

Reverse order content
Display content in reverse

10. Displaying non-printing characters

To view non-printing characters, you need to use -v option.

$ cat -v filename

Conclusion

This leads us to the end of our 10 Linux Cat command examples for Beginners.

Want to share some thoughts? Comment below and let us know!

Divya Kiran Kumar
I'm the Editor of FOSS Linux. I worked as a Software Engineer before taking up blogging as my full-time job. I enjoy using Linux, and can't imagine anything else for my PC. Apart from writing for FOSS Linux, I enjoy reading non-fictional books. Sapiens was my favorite last read. I hope you enjoy reading and using this blog to enhance your Linux experience! Have a great day ahead!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,318FansLike
385FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

Linux Vs. Windows: 10 Key Differences to Know

Merely going by statistics, it is most likely that you are a Windows user.  After all, around  87.70% of all PCs around the world run on Windows with Linux, only accounting for 2.32%.

5 Best Video Players for Linux

Most Linux-based systems come with a built-in video player, but let's be honest, it most definitely won't be able to satisfy all of your requirements, due to the fact that default software is mostly made for basic purposes. In this article, we will review the 5 Best Video Players for Linux and thoroughly discuss their features as well as their pros and cons.

Best Laptops for Linux and Apps Development [2020]

Apart from your programming skills, there are a few other things that can also influence the way you code, and one of them is your computer system for sure. Even though it isn't like you can't code on a regular PC or laptop, speaking from personal experience, you can make the most out of your programming skillset by going for a computer with high specs and one that's been specially designed for such tasks.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.