日期:2014-05-16  浏览次数:20769 次

find后exec与ok区别
本帖最后由 magi1201 于 2013-09-23 12:17:01 编辑
1  查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们
$ find /var/logs -type f -mtime +7 -ok rm { }\ ;

2  查找目录及子目录下最大的5个文件
$ find . -type f -exec ls -s {} \; | sort -n -r | head -5

对find查询出的结果集进行操作时,什么时候参数跟exec, 什么时候跟ok,两者有什么区别呢,请大牛们解答,灰常感谢 
------解决方案--------------------

 -ok command ;
              Like -exec but ask the user first.  If the user agrees, run the command.  Otherwise
              just return false.  If the command is run, its standard input  is  redirected  from
              /dev/null.

              The  response  to  the  prompt  is matched against a pair of regular expressions to
              determine if it is an affirmative or negative response.  This regular expression is
              obtained  from  the system if the `POSIXLY_CORRECT' environment variable is set, or
              otherwise from find's message translations.  If the system has no suitable  defini‐
              tion,  find's  own definition will be used.   In either case, the interpretation of
              the regular expression  itself  will  be  affected  by  the  environment  variables
              'LC_CTYPE'  (character  classes) and 'LC_COLLATE' (character ranges and equivalence
              classes).


从man给出的信息来看,最明显的区别就是-ok是交互式的,在执行后面的命令之前,需要用户确认;-exec不需要确认直接执行。
------解决方案--------------------
直接使用 find ... 
------解决方案--------------------
 xarg ...也行。
------解决方案--------------------
一个交互式,一个非交互式,
shell脚本可以用到-exec
------解决方案--------------------
ok执行前会先询问用户,如果用户同意,才执行命令。
exec不会询问用户,直接执行
另外返回值也不同
------解决方案--------------------
引用:
Quote: 引用:

一个交互式,一个非交互式,
shell脚本可以用到-exec
请详细讲解一下交互式和非交互式,或可举例说明,非常感谢