Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the cat command on Linux system

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use the cat command of the Linux system, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The Linux system cat command is mainly used to connect files and print them to standard output devices. It is a commonly used tool for viewing and connecting text files.

Permissions: all users (that is, root accounts and all ordinary accounts) how to use: cat [parameters] file name

Description:

Cat is a text file viewing and linking tool. To view the contents of a file, it is relatively simple to use cat, that is, the file name is directly followed by cat, such as cat linuxyw.txt.

Cat-help can view cat help information, such as how to use various parameters, of course, you can also use man cat to view it. It is recommended that when you encounter a command that does not know how to use it, use-help or man to view help information and form a good habit. Filename is the file name, that is, the name of the file that needs to be viewed in the system.

Commands with similar functions to this command are: tac,less,tail,more

Parameter description:

-A:-show-all is equivalent to-vET

-b:-number-nonblank for the non-empty output line number, that is, the line number is displayed before each line

-e: equivalent to-vE

-E:-show-ends displays $at the end of each line

-n:-number numbers all lines of the output, that is, the line number is displayed before each line

-s:-squeeze-blank does not output multiple blank lines

-t: equivalent to-vT

-T:-show-tabs displays jump characters as ^ I

-u: (ignored)

-v:-show-nonprinting uses ^ and M-references, except for LFD and TAB

-help displays this help and leaves

Use the example: (in the following example, linuxyw.txt is the name of the file) cat linuxyw.txt looks at the contents of the file named linuxyw.txt, which defaults to three lines, as shown in the following figure:

-A with parameter demonstration: (- show-all is equivalent to-vET), cat-A linuxyw.txt is not commonly used

-b output line number for non-blank lines: cat-b linuxyw.txt

-e is equivalent to-vE (- show-ends displays $at the end of each line), cat-e linuxyw.txt is not commonly used

-n displays the line number cat-n linuxyw.txt for all lines

-s -; squeeze-blank does not output multiple blank lines, in fact, when multiple lines of space are together, only one line of space is output. Not commonly used, other parameters are not demonstrated, you can operate on your own, which is conducive to your own learning.

-help (you can use-help to get help when installing commands or software that you don't understand)

Cat file creation or connection file example: when using cat to create a file, you need to use EOF to end (it is not necessary to use EOF, it is recommended that beginners just know to use EOF) if you use cat to create a file by hand, you need to press the Ctrl+D key combination at the end. As shown in the figure:

When you have finished typing www.linuxyw.com, press the keyboard Ctrl+D to end the input, so that the file is created, and the contents of the file are the two lines of I love linuxyw.com and www.linuxyw.com entered:

When using cat > linuxyw.com, use the > symbol, which means redirect, overwrites the contents of the original file, and is automatically created when there is no file. If you use the > > symbol, that is, the appended content of the table, it will not overwrite the content of the original file, but will only append the content you entered below the content of the original file. Don't get mixed up, there may be a big problem if you use it wrong.

If you do not need manual, you can use the EOF symbol, as shown in the figure: when the first line is used and > >, as in the example above.

If you are a beginner and do not understand the following, you can skip it first and come back later. If you need to generate a configuration file when writing a shell script, but the configuration file has variables, you need to enclose the EOF in single quotes, otherwise the variables will be parsed out. And when writing scripts, EOF can not be followed by other symbols such as spaces, otherwise there will be errors. The following figure shows that when I write a script, I am required to generate the contents under cat into the script configuration file without movement. if EOF is not enclosed in single quotation marks, the following variables such as ${VIP} will be parsed, resulting in an incorrect configuration file.

The following script is that I need to parse the variables when I generate the configuration file and cat so that I don't need to use single quotation marks to enclose it.

Use a simple example: the following is the content of the shell script written: the above cat generates the linux_cat1.txt file, and the following cat generates the linux_cat2.txt file.

After executing the script, we cat to look at these two files.

Cat merges one or more files into one file: for example, remerging the contents of file 1, file 2, and file 3 into file 4. The following figure is an example.

The above is all the contents of this article entitled "how to use cat commands in Linux system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report