linux文件搜索查找命令
linux文件搜索查找命令
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/bash
grep在/etc/passwd文件中查找含有bugboy的每一行,如果找到就把該行輸出到標準輸出上,grep的退出狀態
為0;如果沒找到,grep將不會輸出任何信息,退出狀態為1;如果指定的文件不存在,grep的退出狀態為2。
grep模式支持正則表達式,下麵是一些常用的正則表達式匹配元字符。
^ 行起始錨定符,例如'^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選項,是指按文件名查找,後麵我將會對它進行說明。
命令選項
-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.Po
find命令與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, whereis
which命令在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/find
whereis命令查找源文件、可執行文件、手冊文件的位置。例
[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.gz
which和whereis這兩個命令都可以接受多個參數作為查找命令。
最後更新:2017-04-02 00:06:51