shell - 查找早于 FILE 的文件而不包括 FILE

标签 shell find

我有计划使用 find给我一个比某些 FILE 更旧的文件列表,然后使用 xargs (或 -exec )将文件移动到其他地方。搬东西不是问题; | xargs mv {} ~/trash工作正常。

现在,如果我尝试使用 ! -newer FILE ,然后 FILE包含在列表中,我不想要!

该命令参数的功能确实在逻辑上有意义,因为“不新”可以很好地解释为“相同或旧”,如下所示:

$ find . ! -newer Selection_008.png -exec ls -l {} \;

包含来自比较参数的文件:

-rw-r--r-- 1 and and 178058 2012-09-24 11:46 ./Selection_004.png
-rw-r--r-- 1 and and 16260 2012-09-21 11:25 ./Selection_003.png
-rw-r--r-- 1 and and 38329 2012-10-04 17:13 ./Selection_008.png
-rw-r--r-- 1 and and 177615 2012-09-24 11:53 ./Selection_005.png

(ls -l 仅显示日期以作说明)

我真正需要的 find-older选项,但未在 find(1) 中列出...

当然,可以通过 grep -v 管道输出。 , 使用 sed等,或者利用环境变量来重用文件名(fx. for grep -v),这样我就可以享受 DRY 原则,比如

$ COMP=Selection_008.png find . ! -newer $COMP | grep -v $COMP | xargs ...

但是对于一个单行者来说,这似乎是很多写作,而这不是我想要的。

有比 find 更短/更简单的方法吗?还是我错过了一些选择?我检查了手册页,并搜索了 Google 和 SO...

最佳答案

你可以试试这个:

find . ! -newer $COMP ! -name $COMP

关于shell - 查找早于 FILE 的文件而不包括 FILE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13838845/

相关文章:

bash - 如何在脚本中触发大括号扩展?

mysql - 如何在 Windows 中使用命令提示符从服务器下载 mysql 数据库

visual-studio - Visual Studio 中的全局查找和替换

linux - 对带有日期的变量进行操作会产生错误

linux - 如何在shell脚本中获取函数内部的脚本名称?

bash - 找到通过管道传输到 zcat 然后到 head 的结果

linux - 从 find+xargs+grep 管道中排除 .svn .git 文件

c++ - 普通数组上的 std::lower_bound 和 std::find

javascript - 在字符串中查找div id并删除整个div

shell - Azure powershell cmdlet : Login-AzureRmAccount interactive doesn't work on windows server R8