我正在目录中搜索特定字符串(以查看该字符串所在位置以及文件中的所有实例)。但是,我想从搜索中排除一个特定文件。
这就是正在发生的事情——
$echo "searchstring" > ./old_folder/useless_file
$echo "searchstring" > ./new_folder/good_file
$grep -r --exclude="old_folder/useless_file" searchstring *
./old_folder/useless_file:searchstring
./new_folder/good_file:searchstring
这是我想要的输出 -
./new_folder/good_file:searchstring
答案1
该--exclude
选项采用与文件名匹配的 glob,而不是目录或完整路径:
--exclude=GLOB
Skip files whose base name matches GLOB (using wildcard
matching). A file-name glob can use *, ?, and [...] as
wildcards, and \ to quote a wildcard or backslash character
literally.
所以,你可以这样做:
$ grep -r --exclude="*useless_file*" searchstring
new_folder/good_file:searchstring
或者,排除该目录中的所有文件:
$ grep -r --exclude-dir="old_folder" searchstring
new_folder/good_file:searchstring