1、根據某單詞查找
grep -rn "hello,world!" *
* : 表示當前目錄所有文件,也可以是某個文件名
-r 是遞歸查找
-n 是顯示行號
-R 查找所有文件包含子目錄
-i 忽略大小寫
grep -i pattern files :不區分大小寫地搜索。默認情況區分大小寫,
grep -l pattern files :只列出匹配的文件名,
grep -L pattern files :列出不匹配的文件名,
grep -w pattern files :只匹配整個單詞,而不是字符串的一部分(如匹配‘magic’,而不是‘magical’),
grep -C number pattern files :匹配的上下文分別顯示[number]行,
grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行,
grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。
\< 和 \> 分別標注單詞的開始與結尾。
例如:
grep man * 會匹配 ‘Batman’、‘manic’、‘man’等,
grep '\<man' * 匹配‘manic’和‘man’,但不是‘Batman’,
grep '\<man\>' 只匹配‘man’,而不是‘Batman’或‘manic’等其他的字符串。
'^':指匹配的字符串在行首,
'$':指匹配的字符串在行尾,
2,xargs配合grep查找
find -type f -name '*.php'|xargs grep 'GroupRecord'
3、locate
locate filename:搜索文件和目錄的名稱
locate -i filename:不區分大小寫的搜索文件名和目錄
4、grep
grep want_to_find filename:搜索文件總包含想找的單詞 want_to_find 單詞中間有特殊符號的時候,可以加’’,告訴shell正在搜索一個字符串,使用""則表示要使用shell變量 grep -R want_to_find *:搜索多個目錄的結果 grep -R want_to_find * > want_to_find .txt:把搜索的過多的結果放到want_to_find.txt中 grep -R want_to_find * --color=auto:把搜索出來的單詞變成彩色 grep -i want_to_find *:找出不區分大小寫的單詞 grep -w want_to_find *:在文件中搜索完整的單詞 grep -n want_to_find *:顯示搜索單詞在文件的行數 tail info.log | grep -B 3 "want_to_find " --color=auto 找含有want_to_find 字符的哪一行的前3行(before) tail info.log | grep -A 3 "want_to_find " --color=auto 找含有want_to_find 字符的哪一行的后3行(after) tail info.log | grep -C 3 "want_to_find " --color=auto 找含有want_to_find 字符的前后的完整的上下文信息(context) ls -l | grep -n want_to_find --color=auto 可以把找到的含有want_to_find 字符的那一行的行號打印出來 ls -l | grep -v do_not_want_to_find 可以把不含有do_not_want_to_find 的結果打印出來 grep -il -l want_to_find filename_path/* 可以把含有do_not_want_to_find 的文件名稱打印出來 grep -c 廣告 info.log 把info.log中出現的廣告的次數打印出來 ls -l | grep 196[6-7] | grep -v live 在某個搜索結果中搜索單詞
5.find命令
基本格式:find path expression
1.按照文件名查找
(1)find / -name httpd.conf #在根目錄下查找文件httpd.conf,表示在整個硬盤查找
(2)find /etc -name httpd.conf #在/etc目錄下文件httpd.conf
(3)find /etc -name '*srm*' #使用通配符*(0或者任意多個)。表示在/etc目錄下查找文件名中含有字符串‘srm’的文件
(4)find . -name 'srm*' #表示當前目錄下查找文件名開頭是字符串‘srm’的文件
2.按照文件特征查找
(1)find / -amin -10 # 查找在系統中最后10分鍾訪問的文件(access time)
(2)find / -atime -2 # 查找在系統中最后48小時訪問的文件
(3)find / -empty # 查找在系統中為空的文件或者文件夾
(4)find / -group cat # 查找在系統中屬於 group為cat的文件
(5)find / -mmin -5 # 查找在系統中最后5分鍾里修改過的文件(modify time)
(6)find / -mtime -1 #查找在系統中最后24小時里修改過的文件
(7)find / -user fred #查找在系統中屬於fred這個用戶的文件
(8)find / -size +10000c #查找出大於10000000字節的文件(c:字節,w:雙字,k:KB,M:MB,G:GB)
(9)find / -size -1000k #查找出小於1000KB的文件
3.使用混合查找方式查找文件
參數有: !,-and(-a),-or(-o)。
(1)find /tmp -size +10000c -and -mtime +2 #在/tmp目錄下查找大於10000字節並在最后2分鍾內修改的文件
(2)find / -user fred -or -user george #在/目錄下查找用戶是fred或者george的文件文件
(3)find /tmp ! -user panda #在/tmp目錄中查找所有不屬於panda用戶的文件