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

前端 未结 14 1713
一个人的身影
一个人的身影 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 04:05

    There's also:

    find directory_name -type f -print0 | xargs -0 grep -li word
    

    but that might be a bit much for a beginner.

    find is a general purpose directory walker/lister, -type f means "look for plain files rather than directories and named pipes and what have you", -print0 means "print them on the standard output using null characters as delimiters". The output from find is sent to xargs -0 and that grabs its standard input in chunks (to avoid command line length limitations) using null characters as a record separator (rather than the standard newline) and the applies grep -li word to each set of files. On the grep, -l means "list the files that match" and -i means "case insensitive"; you can usually combine single character options so you'll see -li more often than -l -i.

    If you don't use -print0 and -0 then you'll run into problems with file names that contain spaces so using them is a good habit.

    0 讨论(0)
  • 2020-12-02 04:06
    grep -R "string" /directory/
    

    -R follows also symlinks when -r does not.

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