• Uncategorized

About linux : Less—search-in-multiple-files-at-once

Question Detail

Hi I would like to know if there is possibility to search trough multiple files at once using less. I am facing problem, that I should go trough multiple logs (instance1.log, instance2.log, instance3.log …) and search for some test. Right now I am able to open multiple files in the less using less instance?.log but than I need to manually search trough the files and search each one individually, but I would like to search trough all of them at once. Is there such a option?

Question Answer

From the less man page under the /pattern command:

          Certain characters are special if entered at  the  beginning  of
          the  pattern;  they modify the type of search rather than become
          part of the pattern:

          ^E or *
                 Search multiple files.  That is, if  the  search  reaches
                 the  END of the current file without finding a match, the
                 search continues in the next file  in  the  command  line
                 list.

So for example, use less to open multiple files:

less file1 file2

Then type /^E or /* followed by your search pattern. Use n to navigate forward through pattern matches and shift+n to navigate backward through pattern matches (spanning multiple files).

This is an off-topic question but you can use grep. Check out this answer

I’m not sure if this is what you want, but you can do

cat instance?.log | less

And then search through the result.

Or use grep as it is the tool that is basically made for this use case:

grep -E 'search term' instance?.log

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.