Home Beginner's Guide 10 Linux Cat command examples for beginners

10 Linux Cat command examples for beginners

by Divya Kiran Kumar
cat command usage linux with examples

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!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.