命令行:管道查找结果到 rm [英] Command line: piping find results to rm

查看:21
本文介绍了命令行:管道查找结果到 rm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制定一个删除超过 15 天的 sql 文件的命令.

I'm trying to work out a command which deletes sql files older than 15 days.

find 部分工作,但 rm 不工作.

The find part is working but not the rm.

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

它会准确列出我要删除的文件列表,但不会删除它们.路径是正确的.

It kicks out a list of exactly the files I want deleted but is not deleting them. The paths are correct.

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

我做错了什么?

推荐答案

你实际上是在管道 rmoutputfind.您想要的是使用 find 的输出作为 argumentsrm:

You are actually piping rm's output to the input of find. What you want is to use the output of find as arguments to rm:

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

xargs 是将其标准输入转换"为另一个程序的参数的命令,或者更准确地说,将其放在 man 页面上,

xargs is the command that "converts" its standard input into arguments of another program, or, as they more accurately put it on the man page,

从标准输入构建和执行命令行

build and execute command lines from standard input

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

Note that if file names can contain whitespace characters, you should correct for that:

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

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

But actually, find has a shortcut for this: the -delete option:

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

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

Please be aware of the following warnings in 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.

附注请注意,直接通过管道连接到 rm 不是一种选择,因为 rm 不期望标准输入上的文件名.您目前正在做的是将它们向后管道.

P.S. Note that piping directly to rm isn't an option, because rm doesn't expect filenames on standard input. What you are currently doing is piping them backwards.

这篇关于命令行:管道查找结果到 rm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆