shellscript 02 find & xargs

再谈 find 命令

find pathname –options [-print –exec –ok ]

pathname : 目录

- print : 将匹配的文件输出到标准输出

- exec : 对匹配的文件执行该参数所给出的 shell 命令. 命令参数为 ‘command’ {} \; 注意 {} 和 \ ; 之间的空格

- ok     : 和 –exec 作用相同,只不过以一种更为安全的模式来执行该参数的 shell 命令,在每次执行之前,都会给出提示,让用户来确定是否执行。

find 命令选项

-name : 按照文件名查找

-perm : 按照文件权限查找

-prune : 不在当前目录中查找

-user : 按照文件属主来查找文件

-group : 按照组来查找

-mtime –n +n 按照文件被修改时间, 开可以按照 –atime, –ctime

-nogroup 查找不在 /etc/groups 中不存在

-nawer file1 ! file2 查找更改时间比文件 file1 新 但比文件 file2 旧的文件。

-type 查找某一类型文件 ( b 快设备文件,d 目录,c 字符设备文件,p 管道文件,l 符号链接文件,f 普通文件 )

-depth : 首先查找当前目录中的文件,然后再在其字目录中查找。

-mount : 查找不跨越文件系统 mount 点

-follow : 如果 find 命令遇到符号链接文件,就跟踪至链接所指向的文件。

-cpio : 对匹配的文件使用 cpio 命令,将这些文件备份到设备中。

例子 :

find ~ –name “*.txt”-print

find . –name “[a-z][a-z][0-9][0-9].txt” –print

find . –perm 755 –print ( find . –perm –007 –print )

find /apps –name “/apps/bin” –prune –o –print

find ~ –user dave –print         find ~ –nouser –print

find ~ –group accts –print       find / –nogroup –print

xargs

find 命令和 xargs 命令的用处所在。

find . –name “core” –print | xargs echo “” > /tmp/core.log

posted @ 2012-12-15 10:43  神之一招  阅读(164)  评论(0编辑  收藏  举报