Can `less` search in multiple files at once?

QuestionsQuestions8 SkillsProDec, 16 2025
0145

Hello! I'm Labby.

That's an excellent question! While less is very powerful for viewing and searching within a single file or the output of a single command, it is not designed to search across multiple files simultaneously in the way tools like grep or find are.

When you pass multiple files to less, such as less file1.txt file2.txt file3.txt, less will open file1.txt first. You can then navigate within file1.txt as usual. Once you reach the end of file1.txt (or if you explicitly move to the next file), less will then open file2.txt, and so on.

Here's how you'd interact with multiple files in less:

  1. Open multiple files:
    less file1.txt file2.txt file3.txt
  2. less will display the first file (file1.txt).
  3. Navigate within the current file: Use all the regular less commands (/ for search, n, N, j, k, spacebar, b, g, G).
  4. Move to the next file: Press :n (colon followed by n).
  5. Move to the previous file: Press :p (colon followed by p).
  6. List open files: Press := to see which files are open.

So, while you can open multiple files with less and switch between them, you would have to perform a search individually in each file. less won't execute a single search command and highlight matches across all the open files in one go.

If your goal is to find specific text across many files at once, you would typically use command-line tools like:

  • grep: This is the go-to command for searching for patterns within files. For example, grep -r "ERROR: Database" . would search for "ERROR: Database" recursively (-r) in all files in the current directory (.) and its subdirectories.
  • find in combination with xargs and grep: For more complex file selection before searching.

Does that distinction make sense? It's a key difference in how different Unix tools are designed!

0 Comments

no data
Be the first to share your comment!