How to Use Linux Cat Command

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, run the Linux cat command as follows:

$ cat n

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, 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

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

$ cat -b

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

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 source file, the Linux cat command can be used as follows:

$ cat T

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, run the Linux cat command as follows:

$ cat E

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

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

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.

Sandclock 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, Sandclock 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

Firefox KungFu: Best Firefox Add-ons

Firefox is a very popular web browser among Ubuntu and Linux users and rightly so. It is well known for its add-ons that...

Blogging with Emacs Org-Mode

Thanks to the many plug-ins for Emacs, you can write to your blog from inside your favourite editor. This is useful for...

Install LLVM on Ubuntu

Install LLVM 5 on Ubuntu 17.10 Artful Aardvark LLVM is a C/C++ compiler toolset just like GCC. LLVM can compile C, C++...
Bài Viết

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

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019

How to Update Ubuntu Linux

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu

Hướng dẫn cài đặt Apache CloudStack

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader