find后exec与ok区别

姜小白- 2013-09-23 12:14:54
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,两者有什么区别呢,请大牛们解答,灰常感谢
...全文
369 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pix77 2013-09-27
  • 打赏
  • 举报
回复
引用 7 楼 magi1201 的回复:
[quote=引用 3 楼 mp777323 的回复:] 一个交互式,一个非交互式, shell脚本可以用到-exec
请详细讲解一下交互式和非交互式,或可举例说明,非常感谢 [/quote] 交互式就是一问一答的方式,比方fdisk这样的,如果写在脚本中,放在半夜运行,是不合适的,你总不见得半夜爬起来吧 非交互式,就一次性执行完,途中没有任何要询问你的地方
姜小白- 2013-09-27
  • 打赏
  • 举报
回复
引用 3 楼 mp777323 的回复:
一个交互式,一个非交互式, shell脚本可以用到-exec
请详细讲解一下交互式和非交互式,或可举例说明,非常感谢
姜小白- 2013-09-27
  • 打赏
  • 举报
回复
引用 4 楼 qq864680621 的回复:
ok执行前会先询问用户,如果用户同意,才执行命令。 exec不会询问用户,直接执行 另外返回值也不同
请具体讲一下,返回值不同,如何不同。可举例说明,谢谢
姜小白- 2013-09-27
  • 打赏
  • 举报
回复
引用 2 楼 iasky 的回复:
直接使用 find ... | xarg ...也行。
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...|xargs ... 后,应该怎么写,在执行效率上,哪个更快一些呢,请大神讲解一下
踩踩一哥 2013-09-25
  • 打赏
  • 举报
回复
ok执行前会先询问用户,如果用户同意,才执行命令。 exec不会询问用户,直接执行 另外返回值也不同
糊涂的小白 2013-09-23
  • 打赏
  • 举报
回复

 -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不需要确认直接执行。
pix77 2013-09-23
  • 打赏
  • 举报
回复
一个交互式,一个非交互式, shell脚本可以用到-exec
iasky 2013-09-23
  • 打赏
  • 举报
回复
直接使用 find ... | xarg ...也行。

18,777

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧