在linux下如果要查找包含某个关键字的文件,如要在/root/目录下搜索带有关键字“zcwyou”的文件,在终端下运行命令:
[root@zcwyou ~]# find /root/ –type f |xargs grep "give_flower_log_json"
./20200424/04/give_flower_log.log: Dlogback.configurationFile=/data1/datax/conf/logback.xml -Dlog.file.name=give_flower_log_json
在当前目录下查找带有OutOfMemory关键字的文件
[work@dt-001 dataxLog]$ grep -r 'OutOfMemory' .
./20200422/21/discussion_oper_log.log: - java.lang.OutOfMemoryError: unable to create new native thread
./20200422/21/discussion_oper_log.log:Caused by: java.lang.OutOfMemoryError: unable to create new native thread
查找当前目录及其子目录下所有log文件中包含某关键字的文件,假设关键字为:OutOfMemory
(base) [work@dt-001 dataxLog]$ find -name "*.log" | xargs grep -l 'OutOfMemory'
./20200422/21/discussion_oper_log.log
./20200422/21/exclusive_privilege_signature_log.log
./20200422/21/pyramid_delegate_cancel_log.log
./20200422/21/alliancebuild_log.log
查找目录/var/www/html/test/里,包含带关键字linuxrumen.com的文件并删除这些文件
[root@zcwyou ~]# find /var/www/html/test/ -type f -exec grep “linuxrumen.com” {} \; -print -exec rm {} \;
查找当前目录下的所有文件中是否含有某个字符串,假设关键字为zcwyou
[root@zcwyou ~]# find .|xargs grep -ri "zcwyou"
查找当前目录下的所有文件中是否含有某个字符串(以关键字zcwyou为例),并且只打印出文件名
[root@zcwyou ~]# find .|xargs grep -ri "zcwyou" -l
linux 查找某目录下包含关键字内容的文件
grep -r “{关键字}” {路径}