How to Use Linux Cat Command

29/12/2020
Chưa phân loại
Surprisingly, this is not your everyday pet cat. When you run it, it doesn’t print meow!

The Linux cat command is used to print the contents of a text file. With the Linux cat command, you can print the contents of your c, java source file, Linux configuration files etc.

The cat command is available in every Linux distribution out there by default. So, you don’t have to install it separately.

In this article, I am going to show you how to use the Linux cat command. So, let’s get started.

Basic Usage of Linux Cat Command:

The basic and most common use of the Linux cat command is to use it without any command option.

For example, to view to contents of the /etc/hosts directory, run the cat command as follows:

$ cat /etc/hosts

As you can see, the contents of the /etc/hosts configuration file are printed on the screen.

Printing Line Numbers:

Let’s say, you want to print the contents of a Java source file on the terminal. You can use the cat command of course. But the cat command does not show line numbers by default. For a source file or a program, it is essential. Luckily, the cat command has -n option which you can use to display line numbers.

To display the contents along with the line number of the Java source file Welcome.java, run the Linux cat command as follows:

$ cat n Welcome.java

As you can see, the line numbers are displayed.

Numbering Only Non Blank Lines:

If you want to show line numbers for the lines that are not blank only, you can use the -b option of the Linux cat command.

In the previous Java source file Welcome.java, I’ve added some blank lines just to demonstrate how the -b option works.

As you can see, with the -n option, all the lines (including blank lines) are numbered.

$ cat n Welcome.java

With the -b option, only the lines that are not blank are numbered as you can see in the screenshot below.

$ cat -b Welcome.java

Removing Repeating Empty Lines:

A file you’re trying to view may have lots of empty lines one after the other. This will make the output of cat command very long and annoying.

You can use the -s option of the Linux cat command to remove repeated empty lines as follows:

$ cat s Welcome.java

Printing Tab Characters:

In a source code file of a program, you may have used many tab characters. Fortunately, they are invisible by default. But, if you really need to see all the tab characters you have on your file, then you can use the -T option of the Linux cat command.

Where you might need this feature is when you want to replace all the tab characters with white spaces and you want to make sure that there are not any tab characters left.

To display all the tab characters in our Welcome.java source file, the Linux cat command can be used as follows:

$ cat T Welcome.java

As you can see, the tab characters are displayed as ^I.

Printing End of Line Characters:

If you want to print the EOL (End of Line) character which is represented by $, you can use the -E option of the Linux cat command.

For example, to print the EOL characters of Welcome.java, run the Linux cat command as follows:

$ cat E Welcome.java

As you can see, the EOL characters are printed.

Printing Non-Printing, Tabs, and EOL Characters:

Earlier, you had to use the -v option to print the non-printable characters, use the -T option to print the tab characters, and use the -E option to print the EOL characters. What if you need to print all of these? Well, you can combine all of these options together as follows:

$ cat vTE Welcome.java

But there is a better solution. The Linux cat command has a -A option that does just the same thing with less typing.

$ cat A Welcome.java

As you can see, the outputs are the same.

So, that’s basically how you use Linux cat command to display text files on Linux. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install Xfce on Manjaro Linux

Manjaro Linux is such an awesome Linux distro that brings the Arch Linux into a more user-friendly manner to the community....
29/12/2020

Machine Learning Tutorial with Scikit-learn

In this lesson on Machine Learning with scikit-learn, we will learn various aspects of this excellent Python package which...
29/12/2020

How to Install and Use Hyper Terminal on Ubuntu 18.04 LTS

Hyper is an electron-based terminal. It is written using web technologies such as HTML, CSS, JavaScript etc. It looks very...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025