【问题标题】:Select each file whose name matches one of multiple patterns and is newer than another file选择名称与多个模式之一匹配且比另一个文件新的每个文件
【发布时间】:2020-08-17 17:10:32
【问题描述】:

是否可以使用 find 来显示多个匹配不同名称格式且比日期新的文件名,而不必为 -name 的每个实例显式使用 -newer 标志?我正在尝试将嵌套在 rsync 中的 find 命令从旧脚本从文件黑名单转换为白名单(很快将有更多文件名添加到同一目录中,需要忽略)。

我正在使用作为脚本的一部分更新的锁定文件来查找 mtime 比其 mtime 更新的特定文件:

$ls -la last_script_run.lock
-rw-r--r-- 1 user users 29 Aug 10 00:00 last_script_run.lock

如果我尝试获取多个文件名,如果不对每个 -name 实例使用 -newer 标志,我将无法使其工作。

例如这有效:

find $ORIGIN -type f -name "realm_app*" -newer test/last_script_run.lock \
-or -name "realm_sys*" -newer test/last_script_run.lock

test/logfiles/realm_app_logs_2020_08_10.tgz.closed
test/logfiles/realm_app_logs_2020_08_11.tgz.closed
test/logfiles/realm_app_logs_2020_08_12.tgz.closed
test/logfiles/realm_app_logs_2020_08_13.tgz.closed
test/logfiles/realm_app_logs_2020_08_14.tgz.closed
test/logfiles/realm_app_logs_2020_08_15.tgz.closed
test/logfiles/realm_app_logs_2020_08_16.tgz.closed
test/logfiles/realm_app_logs_2020_08_17.tgz.closed
test/logfiles/realm_system_logs_2020_08_16.tgz.closed

这不是:

find $ORIGIN -type f -name "realm_system*" -newer test/last_script_run.lock \
-or -name "realm_app*"

test/logfiles/realm_app_logs_2020_08_01.tgz.closed
test/logfiles/realm_app_logs_2020_08_02.tgz.closed
test/logfiles/realm_app_logs_2020_08_03.tgz.closed
test/logfiles/realm_app_logs_2020_08_04.tgz.closed
test/logfiles/realm_app_logs_2020_08_05.tgz.closed
test/logfiles/realm_app_logs_2020_08_06.tgz.closed
test/logfiles/realm_app_logs_2020_08_07.tgz.closed
test/logfiles/realm_app_logs_2020_08_08.tgz.closed
test/logfiles/realm_app_logs_2020_08_09.tgz.closed
test/logfiles/realm_app_logs_2020_08_10.tgz.closed
test/logfiles/realm_app_logs_2020_08_11.tgz.closed
test/logfiles/realm_app_logs_2020_08_12.tgz.closed
test/logfiles/realm_app_logs_2020_08_13.tgz.closed
test/logfiles/realm_app_logs_2020_08_14.tgz.closed
test/logfiles/realm_app_logs_2020_08_15.tgz.closed
test/logfiles/realm_app_logs_2020_08_16.tgz.closed
test/logfiles/realm_app_logs_2020_08_17.tgz.closed
test/logfiles/realm_system_logs_2020_08_16.tgz.closed

虽然我提供的两个示例可以使用"realm_*" 的一个实例捕获,但我还有其他几种名称格式无法用-name 的单个实例捕获。为了简洁和可读性,我宁愿只使用一次 -type f 和 -newer $lockfile 部分。我以前使用文件黑名单让它工作:

find $ORIGIN -newer test/last_script_run.lock -type f -not -name \"*csv*\" \
-a -not -name \"*data-collection*\"

现在我正试图将其转换为白名单,我似乎无法让它工作。这是否可行,还是我需要在命令中将-newer 标志添加到-name 的每个实例?

【问题讨论】:

    标签: linux shell find


    【解决方案1】:

    您需要在 OR'ed -name 初选周围加上括号;这样,如果其中任何一个计算结果为 true,并且正在处理的文件比 test/last_script_run.lock 新,则将打印其名称。

    find "$ORIGIN" -type f '(' \
        -name 'realm_app*' -o  \
        -name 'realm_sys*'     \
    ')' -newer test/last_script_run.lock
    

    【讨论】:

      【解决方案2】:

      您可以考虑使用find 来强制执行-newer 标准,然后将结果通过管道传输到grep -f。最终,grep -f 可能被证明是一种更易于维护的方法来指定多个文件名 - 而且您还可以获得正则表达式的好处:

      $ cat file_regexs
      ^realm_app
      ^realm_sys
      
      find "$ORIGIN" -type f -newer test/last_script_run.lock | grep -f file_regexs
      

      也许您使用rsync 可能会使这种方法难以使用,但它可能适用于希望按名称和日期查找文件的其他访问者。

      【讨论】:

        猜你喜欢
        • 2020-12-08
        • 2015-11-22
        • 2013-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        相关资源
        最近更新 更多