系统之家 - 系统光盘下载网站!

当前位置:系统之家 > 系统教程 > Linux find命令-exec参数的作用

Linux find命令中-exec参数的作用(2)

时间:2014-12-31 18:02:54 作者:qipeng 来源:系统之家 1. 扫描二维码随时看资讯 2. 请使用手机浏览器访问: https://m.xitongzhijia.net/xtjc/20141231/33889.html 手机查看 评论

  代码如下:

  [root@localhost test]# ll

  总计 328

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  -rw-r--r-- 1 root root 33 10-28 16:54 log2013.log

  -rw-r--r-- 1 root root 127 10-28 16:51 log2014.log

  lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log

  -rw-r--r-- 1 root root 25 10-28 17:02 log.log

  -rw-r--r-- 1 root root 37 10-28 17:07 log.txt

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxrwx 2 root root 4096 10-28 14:47 test3

  drwxrwxrwx 2 root root 4096 10-28 14:47 test4

  [root@localhost test]# find 。 -type f -mtime +14 -exec rm {} \;

  [root@localhost test]# ll

  总计 312

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxrwx 2 root root 4096 11-12 19:32 test3

  drwxrwxrwx 2 root root 4096 11-12 19:32 test4

  [root@localhost test]#

  说明:

  在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。

  实例3:在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示

  命令:

  find 。 -name “*.log” -mtime +5 -ok rm {} \;

  输出:

  代码如下:

  [root@localhost test]# ll

  总计 312

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxrwx 2 root root 4096 11-12 19:32 test3

  drwxrwxrwx 2 root root 4096 11-12 19:32 test4

  [root@localhost test]# find 。 -name “*.log” -mtime +5 -ok rm {} \;

  《 rm 。。。 。/log_link.log 》 ? y

  《 rm 。。。 。/log2012.log 》 ? n

  [root@localhost test]# ll

  总计 312

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxrwx 2 root root 4096 11-12 19:32 test3

  drwxrwxrwx 2 root root 4096 11-12 19:32 test4

  [root@localhost test]#

  说明:

  在上面的例子中, find命令在当前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。按y键删除文件,按n键不删除。

  实例4:-exec中使用grep命令

  命令:

  find /etc -name “passwd*” -exec grep “root” {} \;

  输出:

  代码如下:

  [root@localhost test]# find /etc -name “passwd*” -exec grep “root” {} \;

  root:x:0:0:root:/root:/bin/bash

  root:x:0:0:root:/root:/bin/bash

  [root@localhost test]#

  说明:

  任何形式的命令都可以在-exec选项中使用。在上面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。

发表评论

0

没有更多评论了

评论就这些咯,让大家也知道你的独特见解

立即评论

以上留言仅代表用户个人观点,不代表系统之家立场

其他版本软件

热门教程

人气教程排行

Linux系统推荐

官方交流群 软件收录