在带有精简的busybox 的系统上查找超过x 天的文件

Rav*_*avi 10 linux find date busybox files

我需要在开发单元中查找和删除超过 1 周的文件。本机上可用的实用程序数量有限。-mtime find的谓词不可用。在这种情况下,如何检查所有早于 x 天的文件?

Sté*_*las 11

-mtime是find(与-delete)的标准谓词,但看起来您有一个精简版的busybox,其中该FEATURE_FIND_MTIME功能已在构建时被禁用。

如果您可以在启用busybox的情况下重建它,您应该能够:

find . -mtime +6 -type f -exec rm -f {} +
Run Code Online (Sandbox Code Playgroud)

或者如果FEATURE_FIND_DELETE也启用:

find . -mtime +6 -type f -delete
Run Code Online (Sandbox Code Playgroud)

如果没有,其他选项可能是在设置为具有一周旧修改时间的文件上使用find -newer(假设FEATURE_FIND_NEWER已启用)。

touch -d "@$(($(date +%s) - 7 * 86400))" ../ref &&
  find . ! -type f -newer ../ref -exec rm -f {} +
Run Code Online (Sandbox Code Playgroud)

或者,如果-newer不可用,但sh的[支持-nt:

touch -d "@$(($(date +%s) - 7 * 86400))" ../ref &&
  find . ! -type f -exec sh -c '
    for f do
      [ "$f" -nt ../ref ] || printf "%s\0" "$f"
    done' sh {} + |
    xargs -0 rm -f
Run Code Online (Sandbox Code Playgroud)