find, grep, sgrep, rgrep, locate, whereis
whereis
命令用來定位指令的二進制程序、源代碼文件和man手冊頁等相關文件的路徑。
whereis命令只能用於程序名的搜索,而且只搜索二進制文件(參數-b)、man說明文件(參數-m)和源代碼文件(參數-s)。如果省略參數,則返回所有信息。
和find相比,whereis查找的速度非常快,這是因為linux系統會將 系統內的所有文件都記錄在一個數據庫文件中,當使用whereis和下面即將介紹的locate時,會從數據庫中查找數據,而不是像find命令那樣,通過遍歷硬盤來查找,效率自然會很高。 但是該數據庫文件並不是實時更新,默認情況下時一星期更新一次,因此,我們在用whereis和locate 查找文件時,有時會找到已經被刪除的數據,或者剛剛建立文件,卻無法查找到,原因就是因為數據庫文件沒有被更新。
whereis [ -sbmu ] [ -SBM dir ... -f ] name...
-b:只查找二進制文件;
-B<目錄>:只在設置的目錄下查找二進制文件;
-f:不顯示文件名前的路徑名稱;
-m:只查找說明文件;
-M<目錄>:只在設置的目錄下查找說明文件;
-s:只查找原始代碼文件;
-S<目錄>只在設置的目錄下查找原始代碼文件;
-u:查找不包含指定類型的文件。
[root@cent6 ~]# whereis tomcat
tomcat: /usr/local/tomcat
locate
https://www.cnblogs.com/xqzt/p/5426666.html
1、命令簡介
locate(locate) 命令用來查找文件或目錄。 locate命令要比find -name快得多,原因在於它不搜索具體目錄,而是搜索一個數據庫/var/lib/mlocate/mlocate.db 。這個數據庫中含有本地所有文件信息。Linux系統自動創建這個數據庫,並且每天自動更新一次,因此,我們在用whereis和locate 查找文件時,有時會找到已經被刪除的數據,或者剛剛建立文件,卻無法查找到,原因就是因為數據庫文件沒有被更新。為了避免這種情況,可以在使用locate之前,先使用updatedb命令,手動更新數據庫。整個locate工作其實是由四部分組成的:
-
/usr/bin/updatedb 主要用來更新數據庫,通過crontab自動完成的
-
/usr/bin/locate 查詢文件位置
-
/etc/updatedb.conf updatedb的配置文件
-
/var/lib/mlocate/mlocate.db 存放文件信息的文件
2、用法
locate [OPTION]... [PATTERN]...
3、選項
-b, --basename match only the base name of path names -c, --count 只輸出找到的數量 -d, --database DBPATH 使用DBPATH指定的數據庫,而不是默認數據庫 /var/lib/mlocate/mlocate.db -e, --existing only print entries for currently existing files -L, --follow follow trailing symbolic links when checking file existence (default) -h, --help 顯示幫助 -i, --ignore-case 忽略大小寫 -l, --limit, -n LIMIT limit output (or counting) to LIMIT entries -m, --mmap ignored, for backward compatibility -P, --nofollow, -H don't follow trailing symbolic links when checking file existence -0, --null separate entries with NUL on output -S, --statistics don't search for entries, print statistics about eachused database -q, --quiet 安靜模式,不會顯示任何錯誤訊息 -r, --regexp REGEXP 使用基本正則表達式 --regex 使用擴展正則表達式 -s, --stdio ignored, for backward compatibility -V, --version 顯示版本信息 -w, --wholename match whole path name (default)
4、示例
示例1: 搜索etc目錄下所有以my開頭的文件
[root@cent6 lib]# locate /etc/my /etc/my.cnf
示例2:新增的文件無法locate,使用updatedb
[root@cent6 ~]# touch new.txt [root@cent6 ~]# locate new.txt [root@cent6 ~]# updatedb [root@cent6 ~]# locate new.txt /root/new.txt
示例3:updatedb的配置文件/etc/updatedb.conf
[root@cent6 ~]# cat /etc/updatedb.conf PRUNE_BIND_MOUNTS = "yes" PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs" PRUNENAMES = ".git .hg .svn" PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache /var/spool/cups /var/spool/squid /var/tmp"
第一行PRUNE_BIND_MOUNTS="yes"的意思是:是否進行限制搜索。
第二行是排除檢索的文件系統類型,即列出的文件系統類型不進行檢索。
第二行表示對哪些后綴的文件排除檢索,也就是列在這里面的后綴的文件跳過不進行檢索。不同后綴之間用空格隔開。
第四行是排除檢索的路徑,即列出的路徑下的文件和子文件夾均跳過不進行檢索。updatedb之后使用locate仍然找不到想要文件
可以檢查掛載的目錄是否被忽略了
find
、命令簡介
find(find) 命令用來在指定目錄下查找文件。任何位於參數之前的字符串都將被視為欲查找的目錄名。如果使用該命令時,不設置任何參數,則find命令將在當前目錄下查找子目錄與文件。並且將查找到的子目錄和文件全部進行顯示。
2、用法
3、選項
'-H'表示只跟隨命令行中指定的符號連接, '-L'表示跟隨所有的符號連接, '-P'是默認的選項,表示不跟隨符號連接。 -D debugoptions. 打印診斷信息 -Olevel Enables query optimisation. 允許查詢優化
EXPRESSIONS:表達式可能由下列成份組成:操作符、選項、測試表達式以及動作:
1、操作符為and、or、!的組合。
! expr True if expr is false.
-not expr Same as ! expr, but not POSIX compliant. expr1 expr2 implied "and"; expr2 is not evaluated if expr1 is false. expr1 -a expr2 Same as expr1 expr2. expr1 -and expr2 Same as expr1 expr2, but not POSIX compliant. expr1 -o expr2 Or; expr2 is not evaluated if expr1 is true.
expr1 -or expr2 Same as expr1 -o expr2, but not POSIX compliant. expr1 , expr2 List; both expr1 and expr2 are always evaluated. The value of expr1 is discarded; the value of the list is the value of expr2.The comma operator can be useful for searching for several different types of thing, but traversing the filesystem hierarchy only once. The -fprintf action can be used to list the various matched items into several different output files.
2、設置項針對這次查找任務,而不是僅僅針對某一個文件,設置項總是返回true;
位置選項 (總是真): -daystart -follow -regextype 普通選項 (總是真,在其它表達式前指定): -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf --version -xdev -ignore_readdir_race -noignore_readdir_race
3、測試項(test)則不同,它針對具體的一個文件進行匹配測試,如-name,-user等,返回true或者false;
測試項
-name -type -ok -newer -perm -atime, -ctime, -depth, -group, -links, -mtime,-nogroup, -nouser, -print, -prune, -size, -user and -xdev -atime, -ctime, -depth, -group, -links, -mtime,-nogroup, -nouser, -perm, -print, -prune, -size,-user and –xdev
測試項說明
-name filename #查找名為filename的文件 -perm #按執行權限來查找 -user username #按文件屬主來查找 -group groupname #按組來查找 -mtime -n +n #按文件更改時間來查找文件,-n指n天以內,+n指n天以前 -atime -n +n #按文件訪問時間來查GIN: 0px"> -ctime -n +n #按文件創建時間來查找文件,-n指n天以內,+n指n天以前 -nogroup #查無有效屬組的文件,即文件的屬組在/etc/groups中不存在 -nouser #查無有效屬主的文件,即文件的屬主在/etc/passwd中不存 -newer f1 !f2 找文件,-n指n天以內,+n指n天以前 -ctime -n +n #按文件創建時間來查找文件,-n指n天以內,+n指n天以前 -nogroup #查無有效屬組的文件,即文件的屬組在/etc/groups中不存在 -nouser #查無有效屬主的文件,即文件的屬主在/etc/passwd中不存 -newer f1 !f2 #查更改時間比f1新但比f2舊的文件 -type b/d/c/p/l/f #查是塊設備、目錄、字符設備、管道、符號鏈接、普通文件 -size n[c] #查長度為n塊[或n字節]的文件 -depth #使查找在進入子目錄前先行查找完本目錄 -fstype #查更改時間比f1新但比f2舊的文件 -type b/d/c/p/l/f #查是塊設備、目錄、字符設備、管道、符號鏈接、普通文件 -size n[c] #查長度為n塊[或n字節]的文件 -depth #使查找在進入子目錄前先行查找完本目錄 -fstype #查位於某一類型文件系統中的文件,這些文件系統類型通常可 在/etc/fstab中找到 -mount #查文件時不跨越文件系統mount點 -follow #如果遇到符號鏈接文件,就跟蹤鏈接所指的文件 -cpio %; #查位於某一類型文件系統中的文件,這些文件系統類型通常可 在/etc/fstab中找到 -mount #查文件時不跨越文件系統mount點 -follow #如果遇到符號鏈接文件,就跟蹤鏈接所指的文件 -cpio #對匹配的文件使用cpio命令,將他們備份到磁帶設備中 -prune #忽略某個目錄
4、動作項(action)則是對某一個文件進行某種動作(最常見的如-print)
actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print -fprint0 FILE -fprint FILE -ls -fls FILE -prune – -quit -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ; -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;
- -print: find命令將匹配的文件輸出到標准輸出。
- -ls:使用ls -dils 格式將匹配的文件輸出到標准輸出。
- -exec: find命令對匹配的文件執行該參數所給出的shell命令。
- -ok: 和-exec的作用相同,只不過以一種更為安全的模式來執行該參數所給出的shell命令,在執行每一個命令之前,都會給出提示,讓用戶來確定是否執行。
4、示例
實例1:-H –L –P指定對符號連接的處理方式
[root@oracledb dir1]# ln -s /bin rootbin [root@oracledb dir1]# find -P . -name pwd [root@oracledb dir1]# find -L . -name pwd ./rootbin/pwd [root@oracledb dir1]# find -H rootbin . -name pwd rootbin/pwd
示例2:根據文件名或者正則表達式進行匹配
列出當前目錄及子目錄下所有文件和文件夾
[root@zabbix alertscripts]# find . ./sendim.py ./sendmail.py ./sendsms.sh ./sendwechat.py
在當前目錄下查找以.py結尾的文件名
[root@zabbix alertscripts]# find . -name "*.py" ./sendim.py ./sendmail.py ./sendwechat.py
在當前目錄下查找以.py結尾的文件名 ,但忽略大小寫
[root@zabbix alertscripts]# find . -iname "*.PY" ./sendim.py ./sendmail.py ./sendwechat.py
當前目錄及子目錄下查找所有以.py和.sh結尾的文件
[root@zabbix alertscripts]# find . -name "*.py" -o -name "*.sh" ./sendim.py ./sendmail.py ./sendsms.sh ./sendwechat.py
找出當前目錄下不是以.sh結尾的文件
[root@zabbix alertscripts]# find . ! -name "*.sh" . ./sendim.py ./sendmail.py ./sendwechat.py
示例3:匹配文件路徑或者文件
find /usr/ -path "*local*"
示例4:基於正則表達式匹配文件路徑
[root@zabbix alertscripts]# find . -regex ".*\(\.sh\|\.py\)$" ./sendim.py ./sendmail.py ./sendsms.sh ./sendwechat.py
同上,但忽略大小寫
[root@zabbix alertscripts]# find . -iregex ".*\(\.SH\|\.PY\)$" ./sendim.py ./sendmail.py ./sendsms.sh ./sendwechat.py
示例5:搜索但跳出指定的目錄
查找當前目錄或者子目錄下所有文件,但是跳過子目錄alertscripts
[root@zabbix zabbix]# find . -path "./alertscripts" -prune -o -name "*.*" . ./web/maintenance.inc.php ./web/zabbix.conf.php ./zabbix_java_gateway.conf ./zabbix_agentd.d ./zabbix_agentd.d/userparameter_mysql.conf ./zabbix_agentd.conf ./alertscripts ./zabbix_java_gateway_logback.xml ./zabbix_server.conf.rpmnew_bak ./zabbix_server.conf_bak ./zabbix_server.conf
示例6:根據文件類型進行搜索
(f 普通文件 l 符號連接 d 目錄 c 字符設備 b 塊設備 s 套接字 p Fifo )
[root@zabbix alertscripts]# find . -type d .
示例7:基於目錄深度搜索 向下最大深度限制為1
[root@zabbix zabbix]# find . -maxdepth 1 . ./web ./zabbix_java_gateway.conf ./zabbix_agentd.d ./zabbix_agentd.conf ./alertscripts ./zabbix_java_gateway_logback.xml ./zabbix_server.conf.rpmnew_bak ./zabbix_server.conf_bak ./zabbix_server.conf
搜索出深度距離當前目錄至少2個子目錄的所有文件
find . -mindepth 2
示例8:根據文件時間戳進行搜索
find . -type f 時間戳
UNIX/Linux文件系統每個文件都有三種時間戳:
訪問時間(-atime/天,-amin/分鍾):用戶最近一次訪問時間。
修改時間(-mtime/天,-mmin/分鍾):文件最后一次修改時間。
變化時間(-ctime/天,-cmin/分鍾):文件數據元(例如權限等)最后一次修改時間。
搜索最近七天內被訪問過的所有文件
find . -type f -atime –7
搜索恰好在七天前被訪問過的所有文件
find . -type f -atime 7
搜索超過七天內被訪問過的所有文件
find . -type f -atime +7
搜索訪問時間超過10分鍾的所有文件
find . -type f -amin +10
找出比file.log修改時間更長的所有文件
find . -type f -newer file.log
示例9:根據文件大小進行匹配
搜索大於10KB的文件
find . -type f -size +10k
搜索小於10KB的文件
find . -type f -size –10k
搜索等於10KB的文件
find . -type f -size 10k
示例10:根據文件權限/所有權進行匹配
當前目錄下搜索出權限為777的文件
find . -type f -perm 777
找出當前目錄下權限不是644的php文件
find . -type f -name "*.php" ! -perm 644
示例11:根據用戶/用戶組進行匹配
找出當前目錄用戶zabbix擁有的所有文件
find . -type f -user zabbix
找出當前目錄用戶組zabbix擁有的所有文件
find . -type f -group zabbix
示例12:–delete 刪除匹配文件
刪除當前目錄下所有.txt文件
find . -type f -name "*.txt" –delete
示例13:列出匹配文件 :
find . -type f -name "*.txt" –ls
5、借助-exec選項與其他命令結合使用
find是我們很常用的一個Linux命令,但是我們一般查找出來的並不僅僅是看看而已,還會有進一步的操作,這個時候exec的作用就顯現出來了。exec選項后面跟隨着所要執行的命令或腳本,然后是一對兒{ },一個空格和一個\,最后是一個分號。
-exec 參數后面跟的是command命令,它的終止是以;為結束標志的,所以這句命令后面的分號是不可缺少的,考慮到各個系統中分號會有不同的意義,所以前面加反斜杠。{} 花括號代表前面find查找出來的文件名。
找出當前目錄下所有root的文件,並把所有權更改為用戶oracle
find .-type f -user root -exec chown oracle {} \;
查找當前目錄下所有.txt文件並把他們拼接起來寫入到all.txt文件中
find . -type f -name "*.txt" -exec cat {} \;> all.txt
將30天前的.log文件移動到old目錄中
find . -type f -mtime +30 -name "*.log" -exec cp {} old \;
找出當前目錄下所有.txt文件並以“File:文件名”的形式打印出來
find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
因為單行命令中-exec參數中無法使用多個命令,以下方法可以實現在-exec之后接受多條命令
-exec ./text.sh {} \;
6、find的ok操作
-ok和-exec行為一樣,不過它會給出提示,讓用戶決定是否執行相應的操作。
找出自己家目錄下所有的.txt文件並刪除
[root@oracledb ~]# find $HOME/. -name "*.txt" -ok rm -f {} \; < rm ... /root/./log1.txt > ? y < rm ... /root/./study/dir2/test.txt > ? y < rm ... /root/./log.txt > ? y [root@oracledb ~]#
7、find與xargs
在使用find命令的-exec選項處理匹配到的文件時, find命令將所有匹配到的文件一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鍾之后,就會出現溢出錯誤。錯誤信息通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。
find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然后是下一批,並如此繼續下去。
在有些系統中,使用-exec選項會為處理每一個匹配到的文件而發起一個相應的進程,並非將匹配到的文件全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統性能下降的問題,因而效率不高;而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數,還是分批取得參數,以及每一次獲取參數的數目都會根據該命令的選項及系統內核中相應的可調參數來確定。
來看看xargs命令是如何同find命令一起使用的,並給出一些例子。
查找系統中的每一個普通文件,然后使用xargs命令來測試它們分別屬於哪類文件
find . -type f -print | xargs file
在整個系統中查找內存信息轉儲文件(core dump) ,然后把結果保存到/tmp/core.log 文件中:
find / -name "core" -print | xargs echo "" >/tmp/core.log
用grep命令在所有的普通文件中搜索hostname這個詞
find . -type f -print | xargs grep "hostname"
刪除3天以前的所有東西
find . -ctime +3 -exec rm -rf {} \;
find ./ -mtime +3 -print|xargs rm -f –r
刪除文件大小為零的文件
find ./ -size 0 | xargs rm -f &
find命令配合使用exec和xargs可以使用戶對所匹配到的文件執行幾乎所有的命令。
grep
http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html
grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹 配的行打印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。
grep的工作方式是這樣的,它在一個或多個文件中搜索字符串模板。如果模板包括空格,則必須被引用,模板后的所有字符串被看作文件名。搜索的結果被送到標准輸出,不影響原文件內容。
grep可用於shell腳本,因為grep通過返回一個狀態值來說明搜索的狀態,如果模板搜索成功,則返回0,如果搜索不成功,則返回1,如果搜索的文件不存在,則返回2。我們利用這些返回值就可進行一些自動化的文本處理工作。
1.命令格式:
grep [option] pattern file
2.命令功能:
用於過濾/搜索的特定字符。可使用正則表達式能多種命令配合使用,使用上十分靈活。
3.命令參數:
-a --text #不要忽略二進制的數據。
-A<顯示行數> --after-context=<顯示行數> #除了顯示符合范本樣式的那一列之外,並顯示該行之后的內容。
-b --byte-offset #在顯示符合樣式的那一行之前,標示出該行第一個字符的編號。
-B<顯示行數> --before-context=<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前的內容。
-c --count #計算符合樣式的列數。
-C<顯示行數> --context=<顯示行數>或-<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前后的內容。
-d <動作> --directories=<動作> #當指定要查找的是目錄而非文件時,必須使用這項參數,否則grep指令將回報信息並停止動作。
-e<范本樣式> --regexp=<范本樣式> #指定字符串做為查找文件內容的樣式。
-E --extended-regexp #將樣式為延伸的普通表示法來使用。
-f<規則文件> --file=<規則文件> #指定規則文件,其內容含有一個或多個規則樣式,讓grep查找符合規則條件的文件內容,格式為每行一個規則樣式。
-F --fixed-regexp #將樣式視為固定字符串的列表。
-G --basic-regexp #將樣式視為普通的表示法來使用。
-h --no-filename #在顯示符合樣式的那一行之前,不標示該行所屬的文件名稱。
-H --with-filename #在顯示符合樣式的那一行之前,表示該行所屬的文件名稱。
-i --ignore-case #忽略字符大小寫的差別。
-l --file-with-matches #列出文件內容符合指定的樣式的文件名稱。
-L --files-without-match #列出文件內容不符合指定的樣式的文件名稱。
-n --line-number #在顯示符合樣式的那一行之前,標示出該行的列數編號。
-q --quiet或--silent #不顯示任何信息。
-r --recursive #此參數的效果和指定“-d recurse”參數相同。
-s --no-messages #不顯示錯誤信息。
-v --revert-match #顯示不包含匹配文本的所有行。
-V --version #顯示版本信息。
-w --word-regexp #只顯示全字符合的列。
-x --line-regexp #只顯示全列符合的列。
-y #此參數的效果和指定“-i”參數相同。
4.規則表達式:
grep的規則表達式:
^ #錨定行的開始 如:'^grep'匹配所有以grep開頭的行。
$ #錨定行的結束 如:'grep$'匹配所有以grep結尾的行。
. #匹配一個非換行符的字符 如:'gr.p'匹配gr后接一個任意字符,然后是p。
* #匹配零個或多個先前字符 如:'*grep'匹配所有一個或多個空格后緊跟grep的行。
.* #一起用代表任意字符。
[] #匹配一個指定范圍內的字符,如'[Gg]rep'匹配Grep和grep。
[^] #匹配一個不在指定范圍內的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一個字母開頭,緊跟rep的行。
\(..\) #標記匹配字符,如'\(love\)',love被標記為1。
\< #錨定單詞的開始,如:'\<grep'匹配包含以grep開頭的單詞的行。
\> #錨定單詞的結束,如'grep\>'匹配包含以grep結尾的單詞的行。
x\{m\} #重復字符x,m次,如:'0\{5\}'匹配包含5個o的行。
x\{m,\} #重復字符x,至少m次,如:'o\{5,\}'匹配至少有5個o的行。
x\{m,n\} #重復字符x,至少m次,不多於n次,如:'o\{5,10\}'匹配5--10個o的行。
\w #匹配文字和數字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零個或多個文字或數字字符,然后是p。
\W #\w的反置形式,匹配一個或多個非單詞字符,如點號句號等。
\b #單詞鎖定符,如: '\bgrep\b'只匹配grep。
POSIX字符:
為了在不同國家的字符編碼中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符類,如[:alnum:]是[A-Za-z0-9]的另一個寫法。要把它們放到[]號內才能成為正則表達式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符類。
[:alnum:] #文字數字字符
[:alpha:] #文字字符
[:digit:] #數字字符
[:graph:] #非空字符(非空格、控制字符)
[:lower:] #小寫字符
[:cntrl:] #控制字符
[:print:] #非空字符(包括空格)
[:punct:] #標點符號
[:space:] #所有空白字符(新行,空格,制表符)
[:upper:] #大寫字符
[:xdigit:] #十六進制數字(0-9,a-f,A-F)
5.使用實例:
實例1:查找指定進程
命令:
ps -ef|grep svn
輸出:
[root@localhost ~]# ps -ef|grep svn
root 4943 1 0 Dec05 ? 00:00:00 svnserve -d -r /opt/svndata/grape/
root 16867 16838 0 19:53 pts/0 00:00:00 grep svn
[root@localhost ~]#
說明:
第一條記錄是查找出的進程;第二條結果是grep進程本身,並非真正要找的進程。
實例2:查找指定進程個數
命令:
ps -ef|grep svn -c
ps -ef|grep -c svn
輸出:
[root@localhost ~]# ps -ef|grep svn -c
2
[root@localhost ~]# ps -ef|grep -c svn
2
[root@localhost ~]#
說明:
實例3:從文件中讀取關鍵詞進行搜索
命令:
cat test.txt | grep -f test2.txt
輸出:
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[root@localhost test]#
說明:
輸出test.txt文件中含有從test2.txt文件中讀取出的關鍵詞的內容行
實例3:從文件中讀取關鍵詞進行搜索 且顯示行號
命令:
cat test.txt | grep -nf test2.txt
輸出:
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test]#
說明:
輸出test.txt文件中含有從test2.txt文件中讀取出的關鍵詞的內容行,並顯示每一行的行號
實例5:從文件中查找關鍵詞
命令:
grep 'linux' test.txt
輸出:
[root@localhost test]# grep 'linux' test.txt
hnlinux
ubuntu linux
linuxmint
[root@localhost test]# grep -n 'linux' test.txt
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test]#
說明:
實例6:從多個文件中查找關鍵詞
命令:
grep 'linux' test.txt test2.txt
輸出:
[root@localhost test]# grep -n 'linux' test.txt test2.txt
test.txt:1:hnlinux
test.txt:4:ubuntu linux
test.txt:7:linuxmint
test2.txt:1:linux
[root@localhost test]# grep 'linux' test.txt test2.txt
test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[root@localhost test]#
說明:
多文件時,輸出查詢到的信息內容行時,會把文件的命名在行最前面輸出並且加上":"作為標示符
實例7:grep不顯示本身進程
命令:
ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"
輸出:
[root@localhost test]# ps aux|grep ssh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
root 16901 0.0 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh
[root@localhost test]# ps aux|grep \[s]sh]
[root@localhost test]# ps aux|grep \[s]sh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
[root@localhost test]# ps aux | grep ssh | grep -v "grep"
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
說明:
實例8:找出已u開頭的行內容
命令:
cat test.txt |grep ^u
輸出:
[root@localhost test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[root@localhost test]#
說明:
實例9:輸出非u開頭的行內容
命令:
cat test.txt |grep ^[^u]
輸出:
[root@localhost test]# cat test.txt |grep ^[^u]
hnlinux
peida.cnblogs.com
redhat
Redhat
linuxmint
[root@localhost test]#
說明:
實例10:輸出以hat結尾的行內容
命令:
cat test.txt |grep hat$
輸出:
[root@localhost test]# cat test.txt |grep hat$
redhat
Redhat
[root@localhost test]#
說明:
實例11:
命令:
輸出:
[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]#
說明:
實例12:顯示包含ed或者at字符的內容行
命令:
cat test.txt |grep -E "ed|at"
輸出:
[root@localhost test]# cat test.txt |grep -E "peida|com"
peida.cnblogs.com
[root@localhost test]# cat test.txt |grep -E "ed|at"
redhat
Redhat
[root@localhost test]#
說明:
實例13:顯示當前目錄下面以.txt 結尾的文件中的所有包含每個字符串至少有7個連續小寫字符的字符串的行
命令:
grep '[a-z]\{7\}' *.txt
輸出:
[root@localhost test]# grep '[a-z]\{7\}' *.txt
test.txt:hnlinux
test.txt:peida.cnblogs.com
test.txt:linuxmint
[root@localhost test]#