13 Cat Command Management file instance Rollup

Source: Internet
Author: User

In a Linux system, most configuration files, log files, and even shell scripts use a text file format, so there are a variety of text editors in the Linux system, but when you just want to look at the contents of these files, you can use a simple command-cat.

This is described in the cat manual:

The cat command reads the contents of the file and outputs it to the standard device.

Cat is a Linux built-in command. Almost all Linux distributions are built in: or I've never heard of a release that doesn't have a built-in cat command. Next, let's start learning how to use it.

1. display file contents

The simplest way is to enter ' cat file_name ' directly.

# Cat/etc/issuecentos Release 5.10 (Final) Kernel \ r on an \m

2. Simultaneous display of line numbers

When reading a configuration file with a lot of content, it is easier to display the line number at the same time, plus the-n parameter can be implemented.

# cat-n/etc/ntp.conf1 # Permit Time synchronization Our time resource but does Not2 # Permit the source to query or modify The service on this system3 restrict default Kod nomodify notrap nopeer noquery4 restrict-6 default Kod nomodify notrap Nopeer noquery56 # Permit All Access over the loopback interface. This could Be7 # tightened as well, but to does so would effect some of The8 # administration FUNCTIONS9 Restrict 127.0.0.11 0 restrict-6:: 1

3. Display line numbers at the beginning of a non-space

Similar to the-n parameter,-B can also display line numbers. The difference is that-B displays line numbers only before non-empty lines.

#cat-B/etc/ntp.conf1 # Permit time synchronization Our time resource but does Not2 # Permit the source to query or modify The service on this system3 restrict default Kod nomodify notrap nopeer noquery4 restrict-6 default Kod nomodify notrap n Opeer Noquery5 # Permit All Access over the loopback interface.  This could Be6 # tightened as well, but to does so would effect some of The7 # administration FUNCTIONS8 Restrict 127.0.0.19 Restrict-6:: 1

4. Show Tab tab

When you want to display the Tab tab in text. You can use the-t parameter. It is identified as ^i in the input results.

# cat-t/etc/hosts# do don't remove the following line, or various programs # that require network functionality would fail. 127.0.0.1^i^ilocalhost.localdomain localhost::1^i^ilocalhost6.localdomain6 Localhost6

5. Show line breaks

The-e parameter uses $ to represent newline characters at the end of each line. As shown below:

# CAT-E/etc/hosts# Don't remove the following line, or various programs$# that require network functionality would fail. $127.0.0.1 localhost.localdomain localhost$::1 localhost6.localdomain6 localhost6$

6. Simultaneous display of tabs and line breaks

When you want to achieve the-t and-e effects at the same time, you can use the-a parameter.

# cat-a/etc/hosts# Don't remove the following line, or various programs$# that require network functionality would fail. $127.0.0.1^i^ilocalhost.localdomain localhost$::1^i^ilocalhost6.localdomain6 localhost6$

7. Split screen Display

When the contents of the file show more than your screen size, you can combine the cat command with other commands to display the screen. Using the pipe character (|) To connect.

# Cat/proc/meminfo | less# Cat/proc/meminfo | More

The difference between the less and more display results is that the less parameter can be pageup and pagedown up and down. And more can only use the space to turn the screen downward.

8. View the contents of 2 files at a time

Located in the/root folder, there are two files named Linux and desktop, each containing the following content:

Linux:ubuntu, CentOS, redhat, mint and Slackware

Desktop:gnome KDE, Xfce, enlightment, and cinnamon

When you want to view the contents of a two file at the same time, you can do this as follows:

# Cat/root/linux/root/desktopubuntucentosredhatmintslackwaregnomekdexfceenlightmentcinnamon

9. Sorting display

Similar. You can also combine the cat command with other commands to customize the output. If combined with sort, the content is sorted by pipe-break display. Example:

# Cat/root/linux | Sortcentosmintredhatslackwareubuntu

10. Input redirection

You can also redirect the display result output to the screen or to another file. You only need to use the > symbol (greater than sign) to generate the output to another file.

# Cat/root/linux >/root/linuxdistro

The above command generates a file called Linuxdistro that is identical to the/root/linux content.

11. Create a new file

There are several ways to create a new file under Linux. The use of cat is one of the methods.

# cat > Operating_systemunixlinuxwindowsmacos

When you enter Cat > operatingsystem, it will generate a operatingsystem file. Then a blank line is displayed below. You can enter the content at this point. For example, we enter UNIX, Linux, Windows and MacOS, and after the input is complete, press ctrl-d to save the cat. At this point you will find that the current folder will generate a file called Operating_system that contains what you just typed.

12. Append content to the file

When you use two > characters, the contents of the first file are appended to the end of the second file. Example:

# cat/root/linux >>/root/desktop# cat/root/desktop

It appends the contents of the/root/linux to the end of the/root/desktop file.

The contents of the second file will look like this:

Gnomekdexfceenlightmentcinnamonubuntucentosredhatmintslackware

13. REDIRECT Input

You can use the < command (less than sign) to enter the file into cat.

# Cat </root/linux

The above command represents the contents of/root/linux as input to cat. The screen appears as follows:

Ubuntucentosredhatmintslackware

In order to be more clear about its meaning, we use the following command:

# Cat </root/linux | Sort > Linux-sort

This command understands: reads the contents from the/root/linux, then sorts, outputs the results and generates linux-sort new files

Then we look at what's in Linux-sort:

Centosmintredhatslackwareubuntu

These are the daily basic applications of some cat commands. More about you can learn from the Cat command manual and remember to practice them often.

13 Cat Command Management file instance Rollup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.