作者:zhanhailiang 日期:2012-12-27
语法说明:
# find path -option [ -print ] [ -exec | -ok | command ] {} \; # -print 将查找到的文件输出到标准输出 # -exec command {} \; -----将查到的文件执行command操作, {} 和 \;之间有空格 # -ok 和-exec相同,只不过在操作前要询用户
场景1:想递归查询某目录下匹配文件,并输出其文件信息
# find . -name "*.php"|xargs ls -al -rwxr--r-- 1 zhanhailiang users 1984 2012-12-18 10:54 ./conf/config.php -rwxr--r-- 1 zhanhailiang users 1131 2012-12-18 10:50 ./conf/sso_config.php -rwxr--r-- 1 zhanhailiang users 2415 2012-12-21 17:11 ./htdocs/about/about.php
今天细研究了下find命令,发现其原生就支持-exec参数,所以可以采取如下方式:
# find . -name "*.php" -exec ls -al {} \; -rwxr--r-- 1 zhanhailiang users 6925 2012-12-18 10:48 ./htdocs/interflight/jump.php -rwxr--r-- 1 zhanhailiang users 15222 2012-12-18 10:48 ./htdocs/interflight/order.php -rwxr--r-- 1 zhanhailiang users 1416 2012-12-18 10:48 ./htdocs/interflight/confirm.php
输出的文件顺序不太一样,如果有必要的,请使用awk&sort对输出流按最后一个参数“文件名”称排序。
其实find本身就具备输出文件信息的参数-ls:
# find . -name "*.php" -ls 3467875 8 -rwxr--r-- 1 zhanhailiang users 6925 12月 18 10:48 ./htdocs/interflight/jump.php 3467876 16 -rwxr--r-- 1 zhanhailiang users 15222 12月 18 10:48 ./htdocs/interflight/order.php 3467877 4 -rwxr--r-- 1 zhanhailiang users 1416 12月 18 10:48 ./htdocs/interflight/confirm.php
场景2:想递归查询某目录下匹配文件里是否包含某关键字都是使用
# find . -name "*.php"|xargs -n 1 grep -in "json_encode" 61:$tpl->assign('userInfo', json_encode($userInfo)); 207: echo json_encode($ret); 238: echo json_encode($ret); 270: echo json_encode($ret);
使用-exec参数:
# find . -name "*.php" -exec grep -in "json_encode" {} \; 61:$tpl->assign('userInfo', json_encode($userInfo)); 207: echo json_encode($ret); 238: echo json_encode($ret); 270: echo json_encode($ret); # find . -name "*.php" -exec grep -in "json_encode" "{}" ";" 61:$tpl->assign('userInfo', json_encode($userInfo)); 207: echo json_encode($ret); 238: echo json_encode($ret); 270: echo json_encode($ret);
注:这里-exec参数可替换成-ok参数,这样每次由用户来确认是否对匹配的文件(目录)执行相应命令,如下(这里只是当做一个例子告诉你可以使用-exec的地方都可以用-ok替换,但一般只有当执行rm之类的操作时才会用到-ok参数来确认)
# find . -name "*.php" -ok grep -in "json_encode" {} \; < grep ... ./htdocs/interflight/jump.php > ? y < grep ... ./htdocs/interflight/order.php > ? y < grep ... ./htdocs/interflight/confirm.php > ? y < grep ... ./htdocs/interflight/select.php > ? y
场景3:系统管理员经常需要根据用户或组来搜索特定用户或组的文件,比如需要查询Web根目录下用户所有者是nobody和root的普通文件:
# find . -user root -group root -ls # find . -user nobody -group nobody -ls
接下来是一些常用场景的总结(来源:Linux下find命令用法)
1)在/logs目录中查找更改时间在5日以前的文件并删除它们: # find logs -type f -mtime +5 -ok rm {} \; 2)查询当天修改过的文件 # find ./ -mtime -1 -type f -exec ls -l {} \; 3)查询文件并询问是否要显示 # find ./ -mtime -1 -type f -ok ls -l {} \; < ls ... ./classDB.inc.php > ? y -rw-r--r-- 1 cnscn cnscn 13709 1月 12 12:22 ./classDB.inc.php # find ./ -mtime -1 -type f -ok ls -l {} \; < ls ... ./classDB.inc.php > ? n 4)在/tmp中查找所有的*.h,并在这些文件中查找"SYSCALL_VECTOR",最后打印出所有包含"SYSCALL_VECTOR"的文件名 A) # find /tmp -name "*.h" | xargs -n 50 grep SYSCALL_VECTOR B) # grep SYSCALL_VECTOR /tmp/*.h | cut -d':' -f1| uniq > filename C) # find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} \; -print 5)# find / -name filename -exec rm -rf {} \; # find / -name filename -ok rm -rf {} \; 6)比如要查找磁盘中大于3M的文件: # find . -size +3000k -exec ls -ld {} \; 4)将find出来的东西拷到另一个地方 # find . -name "*.php" -exec cp '{}' /tmp ';' 7)如果有特殊文件,可以用cpio,也可以用这样的语法: # find dir -name filename -print | cpio -pdv newdir 8)查找2004-11-30 16:36:37时更改过的文件 # find ./ -name "*.php" -exec ls -l --full-time {} \;|grep "2012-12-21 14:19:04"
附录:注意,使用–maxdepth和-name时需要注意传参的顺序,若顺序不对可能会发出警告:
find: warning: you have specified the -maxdepth option after a non-option argument -name, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
zhanhailiang@linux-06bq:~/public_html> find . -name "data.php" -maxdepth 1 -exec php -f {} \; find: warning: you have specified the -maxdepth option after a non-option argument -name, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. // php执行输出 zhanhailiang@linux-06bq:~/public_html> find . -maxdepth 1 -name "data.php" -exec php -f {} \; // php执行输出
参考文档: