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 more, less, and cat commands in Linux to view the contents of a file

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How do I use the more, less, and cat commands in Linux to view the contents of a file? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The commands cat, more and less in Linux can all be used to view the contents of a file. The main differences are:

Cat displays the contents of the entire file at once, and can also connect multiple files to display. It is often used in conjunction with redirection symbols and is suitable for situations where the file content is small.

More and less are generally used to display the content of a file that is more than one screen, and provide the function of turning pages. More is more powerful than cat, providing paging display, while less is more powerful than more, providing commands such as page flipping, jump, search and so on. And both more and less support: display the next page with a space and press b to display the previous page. These three commands are described in detail below.

More file content or output viewing tool:

More is one of our most commonly used tools. The most commonly used tool is to display the output, then page it according to the size of the window, and then prompt the percentage of the file.

Less View File content tool:

The less tool is also a tool for pagination display of files or other output. It should be said that it is a Linux orthodox tool for viewing the contents of files. It is extremely powerful; you are a beginner, and I suggest you use less. Since there is too much content in less, let's introduce the most commonly used ones.

Linux commands cat, more, less can be used to view the contents of the file; cat is an one-time display of the contents of the entire file, but also multiple files can be connected to display, it is often used with redirect symbols, suitable for the case of less file content; more and less are generally used to display the contents of the file more than a screen, and provide the function of turning pages. Less is more powerful than more, providing commands such as page flip, jump, search, etc.

Cat

The cat command displays the contents of the entire file on the screen from top to bottom.

1. Command format

Cat [options]... [file].

2. Command function

Output [file] or standard input combination to standard output.

Cat has three main functions:

1)。 Show the entire file at once: cat filename

2)。 Create a file from the keyboard: cat > filename can only create new files, not edit existing files.

3)。 Merge several files into one file: cat file1 file2 > file

3. List of commonly used parameters

-A,-- show-all equals-vET

-b,-- number-nonblank to the non-empty outgoing line number

-e equals-vE

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

-n,-- number numbers all lines of the output

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

-t is equivalent to-vT

-T,-- show-tabs displays tabbed characters as ^ I.

-u (ignored)

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

-- help displays this help and exits

-- version displays version information and exits

More

The more command will display content page by page, making it easy for users to read page by page, and the most basic command is to press the blank key (space) to display to the next page, and press the b key to return to (back) a page to display, and has the function of searching for strings. The more command reads the file back and forth, so the entire file is loaded at startup.

1. Command format

More [- dlfpcsu] [- num] [+ / pattern] [+ linenum] [file...]

2. Command function

The more command is the same as cat's function is to view the contents of the file, but the difference is that more can view the contents of the file by page, but also supports direct jump line and other functions.

3. List of commonly used parameters

-number of rows displayed by num at a time

-d displays friendly prompts at the bottom of each screen

-l ignores Ctrl+l (page feed). If this option is not given, the more command pauses after displaying a line containing Ctrl+l characters and waits for the command to be received.

-f when calculating the number of lines, it is based on the actual number of lines, not the number of lines after the automatic line wrap (some lines that are too long will be expanded to two or more lines)

-p clear the screen before displaying the next screen.

-c clear the screen from the top and display it.

The successive blank lines in the-s file are compressed into a blank line display.

-u does not show underscore

+ / search for the string first, and then display it after the string

+ num is displayed from line num

4. Common operation commands

Enter goes down n lines and needs to be defined. The default is 1 line

Ctrl+F scrolls down one screen

The space bar scrolls down one screen

Ctrl+B returns to the previous screen

= output the line number of the current line

: F output file name and line number of the current line

V call the vi editor

! Command invokes Shell and executes the command

Q exit more

Less

The less tool is also a tool for pagination display of files or other output. It should be said that it is a tool for linux orthodox viewing the contents of files, which is extremely powerful. The usage of less is more flexible than that of more. In more, we have no way to turn to the front, we can only look back, but if we use less, we can use the function of [pageup] [pagedown] and other keys to go back and forth to browse the file, which is easier to view the contents of a file! In addition, less can have more search functions, not only to search down, but also to search up.

1. Command format:

Less [parameters] file

2. Command function:

Less is similar to more, but you can browse files at will with less, while more can only move forward, but not backward, and less does not load the entire file before viewing it.

3. Command parameters:

-b sets the size of the buffer

-e automatically leave when the file is displayed

-f force the opening of special files, such as peripheral codes, directories, and binaries

-g marks only the last keyword searched

-I ignore the case of the search

-m displays the percentage similar to the more command

-N displays the line number of each line

-o Save the less output in the specified file

-Q does not use warning tones

-s displays a row of continuous empty behavior

-S line will be discarded if it takes too long

-x displays the "tab" key as a specified number space

/ string: the ability to search down for "string"

? Strings: the ability to search up for "strings"

N: repeat the previous search (and / or? Related)

N: reverse repeat the previous search (and / or? Related)

B turn back one page

D turn back half a page

H displays the help interface

Q exits the less command

U scroll forward half a page

Y scroll forward one line

The spacebar scrolls one page

Press enter to scroll one line

For example, when we display the contents of / etc/profile, we ask it to display the line number.

[root@localhost] # less-N / etc/profile what is the Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

After reading the above, have you learned how to use the more, less, and cat commands in Linux to view the contents of the file? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Servers

Wechat

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

12
Report