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

Kubernetes vs Docker

Kubernetes and Docker are on the rise. Both the technologies have had very different roots but have become intertwined...
28/12/2020

How to Install Minecraft Game on Ubuntu 18.04 LTS

Minecraft is an open world game. In Minecraft, you build houses, find foods, battle with enemies and so much more. Minecraft...
28/12/2020

Managing Docker Volumes using Docker Compose

Purpose of Docker Volumes Docker containers are meant to be a drop-in replacement for applications. They are meant to be...
29/12/2020
Bài Viết

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

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024