linux full text search directory


Below are several ways to perform full-text search in a directory on Linux systems, complete with examples and detailed explanations of command options.

1. `grep -rni`

This command combination is perhaps the most well-known. The flags `-r` or `-R` enable recursive search, `-n` adds line numbers to the output, and `-i` allows for case-insensitive search.

grep -rni "search_text" /path/to/directory/

  • - `-r` or `-R`: Recursive search
  • - `-n`: Include line number in output
  • - `-i`: Case-insensitive search

2. `find` + `grep`

This combination is more flexible since `find` is a powerful tool for file searching.

find /path/to/directory/ -type f -exec grep -ni "search_text" {} +

  • - `-type f`: Only search for files
  • - `-exec`: Execute `grep` on each file found
  • - `{}`: Placeholder for the file names found
  • - `+`: End of `-exec` command

3. `ag` (The Silver Searcher)

This is a faster tool than `grep` and is very useful when searching through large codebases.

ag "search_text" /path/to/directory/

4. `ack`

Another alternative to `grep`, commonly used by developers for better syntax highlighting and speed.

ack "search_text" /path/to/directory/

5. `ripgrep` (alias `rg`)

This tool is particularly known for its speed and is straightforward to use.

rg "search_text" /path/to/directory/

6. `locate` + `grep`

If you need to search quickly but don`t need to do it recursively or in real-time, `locate` is a good choice. First, update the database with `updatedb` and then use `grep` on the resulting list.

locate "search_text" | grep "search_text"

  • - `updatedb`: Updates the database for `locate`
  • - `locate`: Searches for all occurrences of the filename

7. `sift`

This is a relatively new search tool that offers fast searching with support for complex expressions.

sift "search_text" /path/to/directory/

Each of these tools has its own advantages and disadvantages, so the best choice depends on the specific situation and requirements.

Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.


Another Questions
    More Information

    BASH scripting course

    We prepared for you video course Marian's BASH Video Training: Mastering Unix Shell, if you would like to get much more information.

    Thank you. Marian Knezek