如何解决选择名称与多个模式之一匹配且比另一个文件新的每个文件
是否可以使用find来显示多个与日期不同的名称格式匹配的文件名,而不必为-newer
的每个实例显式使用-name
标志?我正在尝试将嵌套在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
标志?
解决方法
您需要在“或”后的-名称基元中加上括号;这样,如果其中任何一个评估为true,并且正在处理的文件比 test / last_script_run.lock 更新,则将打印其名称。
find "$ORIGIN" -type f '(' \
-name 'realm_app*' -o \
-name 'realm_sys*' \
')' -newer test/last_script_run.lock
,
您可以考虑使用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
可能会使这种方法难以使用,但对于希望按名称和日期查找文件的其他访问者来说,它可能有用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。