加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

unix – 命令行:pipe查找结果到rm

发布时间:2020-12-15 16:44:06 所属栏目:安全 来源:网络整理
导读:我试图制定一个命令,删除sql文件15天以上。 找到部分是工作,但不是rm。 rm -f | find -L /usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups -type f ( -name '*.sql' ) -mtime +15 它准确地列出了我想要删除的文件,但不是删除它们。路径是正确的。 u
我试图制定一个命令,删除sql文件15天以上。

找到部分是工作,但不是rm。

rm -f | find -L /usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups -type f  ( -name '*.sql' ) -mtime +15

它准确地列出了我想要删除的文件,但不是删除它们。路径是正确的。

usage: rm [-f | -i] [-dIPRrvW] file ...
       unlink file
/usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups/20120601.backup.sql
...
/usr/www2/bar/htdocs/foo/rsync/httpdocs/db_backups/20120610.backup.sql

我究竟做错了什么?

你实际上是管道rm的输出到find的输入。你想要的是使用find的输出作为rm的参数:
find -type f -name '*.sql' -mtime +15 | xargs rm

xargs是将其标准输入转换为另一个程序的参数的命令,或者,它们更准确地将其放在手册页上,

build and execute command lines from standard input

请注意,如果文件名可以包含空格字符,您应该纠正:

find -type f -name '*.sql' -mtime +15 -print0 | xargs -0 rm

但实际上,find有一个快捷方式:-delete选项:

find -type f -name '*.sql' -mtime +15 -delete

请注意以下警告在man find中:

Warnings:  Don't  forget that the find command line is evaluated
  as an expression,so putting -delete first will make find try to
  delete everything below the starting points you specified.  When
  testing a find command line that you later intend  to  use  with
  -delete,you should explicitly specify -depth in order to avoid
  later surprises.  Because -delete  implies  -depth,you  cannot
  usefully use -prune and -delete together.

P.S。注意,直接到rm的管道不是一个选项,因为rm不需要标准输入上的文件名。你目前在做什么是管道他们向后。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读