How can I use grep to find a word inside a folder?

前端 未结 14 1712
一个人的身影
一个人的身影 2020-12-02 03:07

In Windows, I would have done a search for finding a word inside a folder. Similarly, I want to know if a specific word occurs inside a directory containing many sub-directo

相关标签:
14条回答
  • 2020-12-02 03:55

    Similar to the answer posted by @eLRuLL, a easier way to specify a search that respects word boundaries is to use the -w option:

    grep -wnr "yourString" .
    
    0 讨论(0)
  • 2020-12-02 03:58

    Why not do a recursive search to find all instances in sub directories:

    grep -r 'text' *
    

    This works like a charm.

    0 讨论(0)
  • 2020-12-02 04:00
    grep -nr 'yourString*' .
    

    The dot at the end searches the current directory. Meaning for each parameter:

    -n            Show relative line number in the file
    'yourString*' String for search, followed by a wildcard character
    -r            Recursively search subdirectories listed
    .             Directory for search (current directory)
    

    grep -nr 'MobileAppSer*' . (Would find MobileAppServlet.java or MobileAppServlet.class or MobileAppServlet.txt; 'MobileAppASer*.*' is another way to do the same thing.)

    To check more parameters use man grep command.

    0 讨论(0)
  • 2020-12-02 04:01
    1. grep -r "yourstring" * Will find "yourstring" in any files and folders

    Now if you want to look for two different strings at the same time you can always use option E and add words for the search. example after the break

    1. grep -rE "yourstring|yourotherstring|$" * will search for list locations where yourstring or yourotherstring matches
    0 讨论(0)
  • 2020-12-02 04:02

    Don't use grep. Download Silver Searcher or ripgrep. They're both outstanding, and way faster than grep or ack with tons of options.

    0 讨论(0)
  • 2020-12-02 04:04

    The following sample looks recursively for your search string in the *.xml and *.js files located somewhere inside the folders path1, path2 and path3.

    grep -r --include=*.xml --include=*.js "your search string" path1 path2 path3
    

    So you can search in a subset of the files for many directories, just providing the paths at the end.

    0 讨论(0)
提交回复
热议问题