开发者

Use grep to search for a string in files, include subfolders

开发者 https://www.devze.com 2023-01-04 16:59 出处:网络
i have to search for a particular text in files and for that im using grep command but it searches only in current folder.What i want is that using a single grep command i can search a particular 开发

i have to search for a particular text in files and for that im using grep command but it searches only in current folder.What i want is that using a single grep command i can search a particular 开发者_运维技巧thing in the current folder as well as in all of its sub folders.How can i do that???


POSIX grep does not support recursive searching - the GNU version of grep does.

find . -type f -exec grep 'pattern' {} \;

would be runnable on any POSIX compliant UNIX.


man grep says

   -R, -r, --recursive
          Read all  files  under  each  directory,  recursively;  this  is
          equivalent to the -d recurse option.


And even more common is to use find with xargs, say

 find <dir> -type f -name <shellglob> -print0 | xargs grep -0 

where -print0 and -0, respectively, would use null char to separate entries in order to avoid issues with filenames having space characters.

0

精彩评论

暂无评论...
验证码 换一张
取 消