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 less command in Linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces how to use the less command in Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.

1. Preface

There is a way to read a text file on the Linux command line. Cat is probably the most basic command that almost every Linux user knows about this. But cat is not always the best option for displaying the contents of a file.

Suppose you have a text file with more than a thousand lines. Using the cat command, you can simply fill the entire terminal screen with these lines. After using the cat command here, you cannot perform any other operations, such as searching for specific text.

This is the advantage of the less command.

2. What is the `less` command in Linux? Do you need a detailed explanation?

Using the less command, you can read large text files without disrupting your terminal screen. You can also search for text and use it to monitor files in real time.

Some people like to use vim to read large text files. But less is faster than Vim or other similar text editors because it doesn't read the entire file until it starts. Because less is read-only, you have no risk of accidentally editing the file you are viewing.

The syntax of the less command is super simple:

Suppose the file name is filename

Less filename

There are many options for using less's commands, but it's best to focus on the actual usage that is more useful to you.

Let's look at some of the most useful examples of less commands in Linux.

3. Practical case demonstration of `less` command in Linux

Practical case demonstration of less Command in Linux

Let's take a look at some practical examples of how to use the less command in Linux.

It is best to use a large file to understand the use of the less command. Instead of creating a large text file by hand, I recommend copying the existing file from / etc/services to the home directory or wherever you want to practice these commands.

/ etc/services is a large file with hundreds of lines, and once copied, it can be used in practice.

(1) use the `less` command to view the text file

As shown in the following syntax, you can use the less command to view the file as follows:

Less [option]

The output will look like this:

Use the less command to view text files

Note: if you use less to view a small file, you will see an empty blank line at the top. Don't panic. There are no extra lines in the file. It's just the way less commands are displayed.

(2) quit `less`

If you are not used to less commands, it may be difficult to figure out how to exit less. Trust me, it's not complicated at all. Simply press "Q" at any given point to exit less.

I added existing commands earlier so that you can easily follow other less command examples because you need to exit the file between different examples (if you are practicing these commands while you are reading this article).

(3) other examples of `less`

The output of less is divided into several pages. You will only see the text that is populated into the terminal screen.

You can use the up and down arrow keys to move line by line. If you want to move page by page, use the spacebar to move to the next page and the "b" key to return to the previous page.

If you want to move to the beginning of the file, use the "g" key. If you want to see the end of the file, press the "G" key.

Keyboard shortcuts:

Up arrow-move up one line

Down arrow-move down one line

Space or PgDn-move down one page

B or PgUp-Move one page up

G-move to the beginning of the file

G-move to the end of the file

Ng-move to line n

(4) use `less` to display the line number

If you want to see the line number in the less command output, you can use option N as follows:

Less-N

You should be able to see output like this:

Detailed explanation of less command

(5) `less` command to find keywords

If you have a large text file, it is best to search for a specific piece of text rather than reading line by line to find it manually.

To find words, phrases, or even regular expressions, press / and type whatever you want to find.

/ pattern

You can see that the matching keywords will be highlighted:

Less command to find keywords

If there are multiple matches, you can press the "n" key to move to the next matching text. You can use the'N 'key to return to the previous match.

/ pattern performs a forward search. You can use pattern to perform a backward search. Personally, I prefer to do a forward search and press n or n to traverse all matches. Don't worry about searching forward or backward.

By default, searches for lowercase letters are case-sensitive. To ignore case, use the less command with the-I option

Less-I

If you forget to use this option, don't worry. You can also press the-I key to combine before performing a search.

Tip: if you use & pattern, it will only display rows that match the pattern.

(6) the use of `less` command to mark points of interest

When you scroll through a large text file, you find something interesting, but you also need to continue to check the file. How do you remember that interesting point? The answer is marking.

Less allows you to add tags to any line. You can press the "m" key after the letter to add a mark.

Ma

When you want to go back to this tag, simply enter the following command:

(a)

You are not limited to one marked point. You can add multiple tags to the code in less. Just use a different letter for the location of each mark.

(7) use `less` command to monitor and track file changes in real time.

You probably already know that you can use the tail command to monitor log files in real time.

You can also do the same thing with the less command. You can use the + F option to see the new lines added to the file in real time.

Less + F

It displays the last page of the file and waits for new data to be added. Note that regular up and down movements cannot be performed in this mode.

To exit real-time monitoring, press Ctrl+C. In this way, you will return to the normal view of the less command and exit the file normally.

(8) use the `less` command to view multiple files

I'll tell you the truth. This is not my favorite example of using less, but you can do it.

To open multiple files in less time, simply enter the file name one by one:

Less

You will see that it lists the file name and its location in the file list.

View multiple files with the less command

You can use the following keys to view other files in the list:

: n-View the next file in the list

: P-View the previous file in the list

(9) use the `less` command with pipelines

The less command can be used in conjunction with other commands that use pipes. It is especially useful when you know that the output of a command will be very large.

For example, the output of the dmesg command is usually thousands of lines. You don't want it to drown your screen, and you can't analyze the output. With the less pipeline, you will have a more friendly way to read the output.

Dmesg | less

4. Extra hint: use the `less` command to edit files in Linux

No, you can't do that. One of the biggest advantages of the less command is that it provides a "read-only" view. Is that really the case?

If you can't edit text with the less command, why should I use it as a case study to write tutorials? Because when you feel the need to edit the file you are viewing, just press the "v" key.

It opens the file in the default command line text editor on the Linux system. For Ubuntu-based systems, it should be opened in the Nano editor.

Thank you for reading this article carefully. I hope the article "how to use less commands in Linux" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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