How to use Linux cat command

The Linux cat command and print files connected to the standard output device (usually the shell). One of the most common use of cat is to display the file, you can create files instantly, and can perform basic editing directly on the terminal.

How to use Linux cat command

How to create a file using the cat

To create a file using the cat command, enter the following in a terminal window:

[linuxmi@linux:~/www.linuxmi.com]$ cat > linuxmi.txt

When you create a file in this way, the cursor will stay on the new line, you can start typing. This technique provides a good way to start the text file. To finish editing the file, press Ctrl + D. The file is saved as a file name that you use.

To test this procedure by typing the following command is valid:

[Linuxmi @ linux: ~ / www.linuxmi.com] $ ls -lt

You should see a new file, and the size should be greater than 0.

How to use Linux cat command

How to use cat to display files

c When the file is large, the text on the screen quickly flashed (scroll), users often can not see what is displayed. Thus, more generally split-screen display command and the like. To control the scrolling, you can press Ctrl + S to stop scrolling; press Ctrl + Q key recovery scrolling. Press Ctrl + C (interrupt) key to terminate execution of the command, and returns Shell prompt.

at the command also displays a file on the screen. You need to do is remove the greater-than sign, as follows:

[linuxmi@linux:~/www.linuxmi.com]$ cat linuxmi.txt

How to use Linux cat command

To view the document page by page, use the more command:

[linuxmi@linux:~/www.linuxmi.com]$ cat linuxmi.com.py | more

Alternatively, you can also use the less command:

[linuxmi@linux:~/www.linuxmi.com]$ cat linuxmi.com.py | less

How to use Linux cat command

How to display line numbers

For all non-blank lines in the file, use the following command:

[linuxmi@linux:~/www.linuxmi.com]$ cat -b linuxmi.txt

How to use Linux cat command

If the line is no character, no number. To display all aircraft flight lines, regardless of whether they are blank, type the following command:

[linuxmi@linux:~/www.linuxmi.com]$ cat -n linuxmi.txt

How to use Linux cat command

How to display the end of each line

Sometimes, when parsing the data file, programmers will find the problem, because the end of the line there are some unexpected hidden characters, such as spaces. This error will prevent its parser to work.

To display the dollar as end of line characters, enter the following command:

[linuxmi@linux:~/www.linuxmi.com]$ cat -E linuxmi.txt

As an example, consider the following text

When you run this command cat -E command, you receive the following output:

Reduction empty line

When you use the cat command to display the contents of a file, you may not want to see a continuous load of blank lines. Use -s switch all compressed into a blank line blank line:

[linuxmi@linux:~/www.linuxmi.com]$ cat -s linuxmi.txt

How to use Linux cat command

How to display labels

When displaying documents using the separator tabs, typically you not see these tabs.

The following command will display TAB characters ^ the I , which makes them easier to see:

[linuxmi@linux:~/www.linuxmi.com]$ cat -T linuxmi.txt

How to use Linux cat command

Series multiple files

The cat is the focus of the series. Use the following command to connect multiple files to the screen:

How to use Linux cat command

To the series file and create a new file, use the following command:

cat linux.txt ubuntu.txt > linuxmi.txt

How to use Linux cat command

Display file in reverse order

Use the following command to display the file in reverse order:

[linuxmi@linux:~/www.linuxmi.com]$ tac linuxmi.txt

How to use Linux cat command

Technically, this is not the cat command, which is the tac command, but actually perform the same operation in the opposite direction. Is the content of the output file in reverse order, the last line of the file is displayed in the first line.

Guess you like

Origin www.linuxidc.com/Linux/2020-02/162270.htm