您的位置:首页 > 运维架构 > Linux

linux文件搜索查找命令

2011-10-09 09:44 399 查看
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这两个命令都可以接受多个参数作为查找命令。

下面是替换一个目录下所有文件中某一字符串的命令:

sed -i "s/zhangsan/lisi/g" `grep zhangsan -rl 目录`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: