linux - 移动具有特定修改日期的文件; "find | xargs ls | grep | -exec"失败 w/"-exec: command not found"

标签 linux centos

我使用centos 7

如果我想查找具有特定名称和特定日期的文件,则将这些文件移动到另一个文件夹,然后发出命令

find -name 'fsimage*' | xargs ls -ali | grep 'Oct 20' | -exec mv   {}  /hdd/fordelete/  \;

出现以下错误

-bash: -exec: command not found xargs: ls: terminated by signal 13

最佳答案

正如另一个答案已经解释的那样,-execfind 的操作,您不能将其用作 shell 命令。相反,xargsgrep 是命令,您不能将它们用作 find 操作,就像您不能在 find 中使用管道 | 一样。

但更重要的是,即使您可以find 的结果使用 lsgrep 只是为了移动早于某个时间的文件,但您不应该。这种管道很脆弱,在许多极端情况下都会失败,例如符号链接(symbolic link)、名称中带有换行符的文件等。

请使用查找。你会发现它非常强大。

例如,对于 7 天前修改的 mv 文件,请使用 -mtime test :

find -name 'fsimage*' -mtime +7 -exec mv '{}' /some/dir/ \;

mv 文件 modified on a specific/reference date ,例如2017-10-20,您可以使用 -newerXY test :

find -name 'fsimage*' -newermt 2017-10-20 ! -newermt 2017-10-21 -exec mv '{}' /some/dir/ \;

此外,如果您的 mv 支持 -t 选项(先给出目标目录,然后给出多个文件),您可以在 find 中使用 {} + 占位符查找多个文件,从而减少 mv 命令调用的总数(感谢 @CharlesDuffy):

find -name 'fsimage*' -mtime +7 -exec mv -t /some/dir/ '{}' +

关于linux - 移动具有特定修改日期的文件; "find | xargs ls | grep | -exec"失败 w/"-exec: command not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47001437/

相关文章:

c - shmat 返回段错误(核心已转储)

linux - Ansible Vault 不读取定义的变量

php - 服务器停止确认 php 更改

linux - AWS EFS : How to browse file?

linux - 配置 apache 服务器以访问文件以供 php 执行

php - 如何使用 PHP 创建一个关机按钮来关闭计算机?

centos - 如何在 VPS 上设置 CentOS 端口转发?

linux - 如何将初始化脚本添加到我的 RPM

centos - 如何在 CentOS 7 中将 libxml2 升级到 2.9.9 版本?

c - gdb: 未定义的输出格式 "z"