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 less to view files in linux

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Xiaobian to share with you how to use less to view files in linux, I believe most people still do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

LESS environmental variables

If you define the environment variable LESS (e.g. in.bashrc), less treats it as a list of options, just as it is passed to it on the command line.

I define it this way:

LESS='-C -M -I -j 10 -# 4'

It means:

C-Speeds up full-screen printing by not scrolling from the bottom.

-M-Displays more information on the last line (status line). You can customize the information displayed using-PM, but I usually just use-M.

-I-Ignore case in search.

-j 10-Display search results on line 10 of the terminal instead of the first line. Thus, each time you press n or (N) to jump to the next (or previous) match, there are 10 contexts.

- #4-When the arrow to the right or left is pressed, it jumps four characters to the right or left. By default it jumps half a screen, which I think is too much. In general, less seems (at least in part) to be optimized for the environment in which it was originally developed, using slow modems and low-bandwidth internet connections, so skipping half a screen makes sense.

PAGER environmental variables

Many programs use commands in the PAGER environment variable to display information. So you can set PAGER=less in.bashrc and have the program run less. Check the man page (man 7 environ) for other such variables.

-S

-S tells less to cut long lines instead of wrapping them. I rarely need to unless I am (or want to be) looking at documents. Fortunately, you can type all command-line options in less as if they were keyboard commands. So if I want to break a long line when the file is already open, I can simply type-S. LCTT: Note capital S and press Enter.

Here's an example I use often:

su - postgresexport PAGER=less #because I don't have to edit postgres.bashrcpsql on all machines

Sometimes when I look at the output of a SELECT command that is very wide, I type-S to format it nicely. If it jumps too far when I press the right arrow to see more (because I didn't set-#), I can type-#8, which moves eight characters to the right every time I press the right arrow.

Sometimes after typing-S multiple times, I exit psql and run it again after setting environment variables:

export LESS=-SF

Command F makes less work like tail -f, waiting for more data to be added to the file before displaying it. One advantage over tail -f is that highlighting search matches is still valid. So you can type less /var/log/logfile, which will highlight everything that appears when you search for something (unless you use-g), and then press F, which will display less and highlight new matches when more data is written to the log.

After pressing F, you can press Ctrl+C to stop it from finding new data (this doesn't kill it), so you can go back to the file to view old content, search for other content, etc., and then press F again to view more new data.

search

Search uses a systematic regular expression library, which usually means you can use extended regular expressions. Specifically, search for one.| two| Three can find and highlight all one, two or three.

Another pattern that I often use is.* someting.*,Especially for long log lines (for example, lines that span multiple terminal widths), it highlights the entire line. This pattern makes it easier to see where a line starts and ends. I also combine other content, such as.* one thing.*|.* another thing.*,Or use key: .*|.* marker.* Check the contents of the key. For example, a log file contains a dump of dictionaries/hashes. It highlights the relevant tag line, so I can see the context, and even, if I know the value is enclosed in quotes, I can:

key: '[^']*'|.* marker.*

Less keeps a history of your search terms and saves them to disk for future calls. When you press/or? You can use the up or down arrows to browse through the history (and make basic line edits).

While writing this article, I stumbled across the less man page and found a very useful feature: using &! Pattern skips unrelated rows. For example, when I look for content in/var/log/messages, I often iterate through the following commands:

cat /var/log/messages | egrep -v 'systemd: Started Session' | lesscat /var/log/messages | egrep -v 'systemd: Started Session|systemd: Starting Session' | lesscat /var/log/messages | egrep -v 'systemd: Started Session|systemd: Starting Session|User Slice' | lesscat /var/log/messages | egrep -v 'systemd: Started Session|systemd: Starting Session|User Slice|dbus' | lesscat /var/log/messages | egrep -v 'systemd: Started Session|systemd: Starting Session|User Slice|dbus|PackageKit Daemon' | less

But now I know how to do the same thing in less. For example, I can type &! systemd: Started Session, and then I want to hide systemd: Starting Session, so I type &!, And use the up arrow to get the results of the last search from the history. And then I type in| systemd: Starting Session and pressing Enter, continue adding more entries in the same way until I filter enough entries to see something more interesting.

=

Command = shows more information about files and locations, even more than-M. If the file is very large and computing = takes too long, press Ctrl+C and it will stop trying to compute.

If the content you are viewing comes from a pipe rather than a file,=(and-M) does not display unknown content, including the number of rows and bytes in the file. To view this data, if you know that the pipe command will end soon, you can skip to the end with G, and then less will start displaying this information.

If G is pressed and the command to write to the pipe takes longer than expected, you can press Ctrl+C and the command will be terminated. Ctrl+C will kill it even if you don't press G. So if you don't want to terminate the command, don't accidentally press Ctrl+C. For this reason, if the command does something (not just display information), it is usually safer to write its output to a file and view the file in a separate terminal rather than using pipes.

Why you need less

Less is a very powerful program, unlike newer competitors in the field (e.g. most and moar), and you'll probably find it on almost any system, just like vi. So even if you use a GUI viewer or editor, it's worth spending some time browsing through the less man page to at least get a sense of what it's for. That way, when you need to do something that an existing feature might provide, you know how to search the manual page or the Internet to find what you need.

The above is "how to view files with less in linux" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to 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

Servers

Wechat

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

12
Report