Solaris檔案搜尋及字串尋找命令 1、grep功能描述 grep 命令在一個或多個檔案中尋找與指定模式比對的字串。如果模式裡包含有空格,必須用引號括起來。grep的模式只能是一個被引號括起來的字串或者是一個單詞,後面緊跟著的參數都被當作檔案名稱。grep命令把結果輸出到標準輸出上,並不改變被搜尋的源檔案。命令格式 grep pattern filename filename2 ...grep有幾個選項比較常用的 -i 尋找時忽略大小寫進行比較 -n 顯示找到的行在檔案中的行號 -v 顯示不匹配的行例1:在/etc/passwd檔案中尋找包含"bugboy"的行 [bugboy@bugboy ~]$ grep bugboy /etc/passwd bugboy:x:500:500:YanDingcheng:/home/bugboy:/bin/bashgrep在/etc/passwd檔案中尋找含有bugboy的每一行,如果找到就把該行輸出到標準輸出上,grep的退出狀態為0;如果沒找到,grep將不會輸出任何資訊,退出狀態為1;如果指定的檔案不存在,grep的退出狀態為2。grep模式支援Regex,下面是一些常用的Regex匹配元字元。^ 行起始錨定符,例如'^love',匹配所有以love開始的行。 $ 行結束錨定符,例如'love$',匹配所有以love結束的行。 . 匹配任意一個字元, 例如'l..e',匹配所有以"l"開頭,緊跟兩個字元,然後以"e"結尾的字串。 * 匹配0個或多個任一字元,例如' *bug',匹配以任意多個空格開始,後跟"bug"的字串。 [] 匹配字元集中的一個字元,例如'[Bb]ook',匹配Book或book。 [^] 匹配不在字元集中的一個字元,例如'[^A-Z]low',匹配所有不以大寫字母開頭,後跟"low"的字串。 /< 單詞超始錨定符,例如'/<go',匹配所有以"go"開頭的單詞。 /> 單詞結束錨定符,例如'or/>',匹配所有以"or"結束的單詞。 x/{m/} 匹配m個x,例如'o/{5}',匹配5個o,即"ooooo"。 x/{m,/} 匹配至少m個x,例如'o/{5,/},匹配至少5個o,即"ooooo","oooooo"等。 x/{m,n/} 匹配m到n個x,例如'o/{2,5/}',匹配2到5個為,即"oo","ooo","oooo","ooooo"。例2:在/etc/passwd中尋找使用者名稱以"b"開頭的使用者 [bugboy@bugboy test]$ grep ^b /etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin bugboy:x:500:500:YanDingcheng:/home/bugboy:/bin/bash例3:在/etc/passwd中尋找SHELL為bash的使用者 [bugboy@bugboy test]$ grep bash$ /etc/passwd root:x:0:0:root:/root:/bin/bash ftp:x:14:50:FTP User:/var/ftp:/bin/bash netdump:x:34:34:Network Crash Dump user:/var/crash:/bin/bash pvm:x:24:24::/usr/share/pvm3:/bin/bash mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash bugboy:x:500:500:YanDingcheng:/home/bugboy:/bin/bash例3:在/etc/passwd中尋找含有"00"的行 [bugboy@bugboy test]$ grep '0/{2/}' /etc/passwd games:x:12:100:games:/usr/games:/sbin/nologin bugboy:x:500:500:YanDingcheng:/home/bugboy:/bin/bash例4:尋找sscanf.c檔案中的非空行 [bugboy@bugboy test]$ cat sscanf.c #include <stdio.h> #include <stdlib.h>int main(int argc, char *argv[]) { const char *str = "I am bugboy"; char mystr[256]; sscanf(str, "%s", mystr); printf("1: %s/n", mystr); char key[64], value[64]; sscanf(str, "%s %s", key, value); printf("key: %s, value: %s/n", key, value); return 0; } [bugboy@bugboy test]$ grep -v '^ *$' sscanf.c #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { const char *str = "I am bugboy"; char mystr[256]; sscanf(str, "%s", mystr); printf("1: %s/n", mystr); char key[64], value[64]; sscanf(str, "%s %s", key, value); printf("key: %s, value: %s/n", key, value); return 0; }例5:尋找sscanf.c檔案中含有"const char"的行,注意,如果要尋找的字串不只一個單詞,要用引號括起來。 [bugboy@bugboy test]$ grep "const char" sscanf.c const char *str = "I am bugboy"; 2、find功能描述 find命令在檔案系統中尋找檔案命令格式 find [path ...] [option] [-exec | -ok | -print]find 命令的參數 path find命令所尋找的目錄路徑。 -exec find命令對尋找到的每一個匹配檔案執行一個shell命令,命令格式為 "-exec command {} /;", 注意“{}”和“/;”之間有一個空格,最後的“;“也不要忘了。 -ok 和-exec的作用相同,只是在執行命令之前請求使用者確認,更安全的執行命令。 -print 將尋找到的檔案輸出到標準輸出。例1:查到/tmp目錄下所有副檔名為".tmp"的檔案並刪除。 [bugboy@bugboy test]$ find /tmp -name "*.tmp" -exec rm {} /; 這裡用到了一個-name選項,是指按檔案名稱尋找,後面我將會對它進行說明。命令選項option -name 按照檔案名稱尋找檔案。 -perm 按照檔案許可權來尋找檔案。 -user 按照檔案屬主來尋找檔案。 -group 按照檔案所屬的組來尋找檔案。 -mtime -n +n 按照檔案的更改時間來尋找檔案,-n表示檔案更改時間距現在n天以內,+n表示檔案更改時間距現在n天以前。 find命令還有-atime和-ctime選項,它們和-mtime選項類似。 -nogroup 尋找無有效所屬組的檔案,即該檔案所屬的組在/etc/groups中不存在。 -nouser 尋找無有效屬主的檔案,即該檔案的屬主在/etc/passwd中不存在。 -newer file1 ! -newer file2 尋找更改時間比檔案file1新但比檔案file2舊的檔案。 -type 尋找某一類型的檔案,諸如: b - 塊裝置檔案。 d - 目錄。 c - 字元裝置檔案。 p - 管道檔案。 l - 符號連結檔案。 f - 普通檔案。 -size n:[c] 尋找檔案長度為n塊的檔案,帶有c時表示檔案長度以位元組計。 -depth 在尋找檔案時,首先尋找目前的目錄中的檔案,然後再在其子目錄中尋找。 -fstype 尋找位於某一類型檔案系統中的檔案,這些檔案系統類型通常可以在設定檔/etc/fstab中找到, 該設定檔中包含了本系統中有關檔案系統的資訊。 -mount 在尋找檔案時不跨越檔案系統mount點。 -follow 如果find命令遇到符號連結檔案,就跟蹤至連結所指向的檔案。 -cpio 對匹配的檔案使用cpio命令,將這些檔案備份到磁帶裝置中。另外,下面三個的區別: -amin n 尋找系統中最後N分鐘訪問的檔案 -atime n 尋找系統中最後n*24小時訪問的檔案 -cmin n 尋找系統中最後N分鐘被改變檔案狀態的檔案 -ctime n 尋找系統中最後n*24小時被改變檔案狀態的檔案 -mmin n 尋找系統中最後N分鐘被改變檔案資料的檔案 -mtime n 尋找系統中最後n*24小時被改變檔案資料的檔案例2:尋找目前的目錄下的塊裝置檔案 [bugboy@bugboy dev]$ find . -type b ./fd0H720 ./fd0H360 ./fd0H1440 ./fd0D720 這裡省略掉了部份結果。例3:尋找目前的目錄下5天以內修改過的檔案 [bugboy@bugboy test]$ find . -mtime -5 . ./toto.s ./toto.c ./tookit ./over ./over.s ./over.c ./tookit.c例3:尋找目前的目錄下比stat.c檔案新,比over.c檔案舊的檔案 [bugboy@bugboy test]$ find . -newer stat.c ! -newer over.c ./toto.s ./crypt ./test ./toto.c ./strtok ./foo.h ./fstatvfs ./strsep.c例4:尋找目前的目錄下,具有644(使用者可讀、寫,組可讀,其它使用者可讀)許可權的檔案 [bugboy@bugboy test]$ find . -perm 644 ./auto/gnip-1.0.tar.gz ./auto/gnip.o ./auto/.deps/gnip.Pofind命令與xargs命令相結合使用在使用find命令的-exec選項處理匹配到的檔案時, find命令將所有匹配到的檔案一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鐘之後,就會出現溢出錯誤。錯誤資訊通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。find命令把匹配到的檔案傳遞給xargs命令,而xargs命令每次只擷取一部分檔案而不是全部,不像-exec選項那樣。這樣它可以先處理最先擷取的一部分檔案,然後是下一批,並如此繼續下去。在有些系統中,使用-exec選項會為處理每一個匹配到的檔案而發起一個相應的進程,並非將匹配到的檔案全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統效能下降的問題,因而效率不高;而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次擷取所有的參數,還是分批取得參數,以及每一次擷取參數的數目都會根據該命令的選項及系統核心中相應的可調參數來確定。例5:尋找目前的目錄下所有的.c檔案,並在檔案中搜尋stat字串,輸出包含stat的行和行號 [bugboy@bugboy test]$ find . -name "*.c" | xargs grep -n stat ./stat.c:3:#include <sys/stat.h> ./stat.c:5:#include <sys/statvfs.h> ./stat.c:9: struct statvfs fsd; ./stat.c:11: statvfs("/", &fsd); ./fstatvfs.c:2:#include <sys/statvfs.h> ./fstatvfs.c:4:#include <sys/stat.h> ./fstatvfs.c:10: struct statvfs vfs; ./fstatvfs.c:14: if (fstatvfs(fd, &vfs) < 0) { ./fstatvfs.c:15: fprintf(stderr, "fstatvfs error()./n"); ./statl.c:2:#include <sys/stat.h> ./statl.c:8: struct stat statbuf; ./statl.c:15: stat(argv[1], &statbuf); ./statl.c:16: if (S_ISDIR(statbuf.st_mode)) { ./statl.c:18: } else if (S_ISBLK(statbuf.st_mode)) { ./test.c:7:static int get_netmask_len(const char *netmask)3、which, whereiswhich命令在PATH環境變數中尋找可執行檔,並列印出檔案的全路徑。例 [bugboy@bugboy test]$ which test /usr/bin/test [bugboy@bugboy test]$ which ls man find alias ls='ls --color=tty' /bin/ls /usr/bin/man /usr/bin/findwhereis命令尋找源檔案、可執行檔、手冊檔案的位置。例 [bugboy@bugboy test]$ whereis test test: /usr/bin/test /usr/share/man/man1p/test.1p.gz /usr/share/man/man1/test.1.gz [bugboy@bugboy test]$ whereis ls find ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz find: /usr/bin/find /usr/share/man/man1p/find.1p.gz /usr/share/man/man1/find.1.gzwhich和whereis這兩個命令都可以接受多個參數作為尋找命令。