是否可以使用find来显示多个文件名,以匹配比日期新的不同名称格式,而不必对每个-name实例显式使用-name标志?我正在尝试将嵌套在rsync中的find命令从一个较旧的脚本从文件黑名单转换为一个白名单(设置了更多的文件名,很快就会添加到需要忽略的同一个目录中)。
我使用一个锁文件作为脚本的一部分进行更新,以找到比mtime更新更新的特定文件:
$ls -la last_script_run.lock
-rw-r--r-- 1 user users 29 Aug 10 00:00 last_script_run.lock如果我试图获取多个文件名,如果不对每个-newer实例使用-name标志,就无法使其工作。
例如,:
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实例中?
发布于 2020-08-17 17:20:32
您需要在OR‘’ed -name主目录周围加上括号;这样,如果其中任何一个计算结果为true,并且正在处理的文件比更新,则将打印其名称。
find "$ORIGIN" -type f '(' \
-name 'realm_app*' -o \
-name 'realm_sys*' \
')' -newer test/last_script_run.lock发布于 2020-08-18 01:06:34
您可以考虑使用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的使用可能会使这种方法难以使用,但它可能适用于其他希望通过名称和日期查找文件的访问者。
https://stackoverflow.com/questions/63455533
复制相似问题