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 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. 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,872FansLike
267FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to create an Arch Linux Live USB drive on Ubuntu

create Arch Linux Live USB drive
This article provides a step-by-step guide on creating an Arch Linux USB installation media on Ubuntu PC. The Live USB media can be used to install Arch Linux.
linux vs unix
When discussing Linux and Unix with average users, it's not uncommon that they will sometimes mistakenly interchange the terms Linux and Unix.  The two are not the same.  Though they share similarities in their overall structures and toolkits, they are decidedly not the same.
best linux distros programmers
Linux distros have long been a favorite among programmers since the rise in popularity of the OS in the nineties. Programmers are technical by nature, and Linux distros appeal to that technical nature. Let's discuss why Linux is a great desktop OS for programmers and developers, and find out best distros suitable for them.