Jakub Znamenáček
Jakub Znamenáček

Reputation: 838

Less - search in multiple files at once

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?

Upvotes: 2

Views: 2902

Answers (3)

Aaron Jones
Aaron Jones

Reputation: 131

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).

Upvotes: 2

nullptr
nullptr

Reputation: 4484

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

Upvotes: 0

Javier Galarza
Javier Galarza

Reputation: 48

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

Upvotes: 0

Related Questions