假设我有这些文件:
file-foo-1514764800.log
file-foo-1514851200.log
file-foo-1514937600.log
file-foo-1515024000.log
file-foo-1515110400.log
file-bar-1514764800.log
file-bar-1514851200.log
file-bar-1514937600.log
file-bar-1515024000.log
file-bar-1515110400.log
文件名中的时间戳对应于 1 月 1 日至 1 月 5 日。如果我想过滤时间戳在 Jan 2nd
范围内的文件最多 Jan 4th
,我需要写一个表达式,例如 >= 1514851200 && <= 1515024000
(>= Jan 2nd && <= Jan 4th),如果我们使用 -
,则使用它过滤文件名中的第三项。作为分隔符。
请注意,在我的例子中,我不能依赖文件的修改日期,因为它们可能在任意时间被修改过。在这种情况下,解决方案相当简单:
find . -maxdepth 1 -newermt "2018-01-04" ! -newermt "2017-01-06"
有什么简单的方法可以使用 bash(zsh 也可以)和常用的 linux 工具来解决这个问题?
最佳答案
我有一个想法,虽然不完美但很简单。转到日志文件目录并运行以下命令:
for f in *.log; do m=${f/*-*-}; n=${m/.log}; [[ "$n" -ge 1514851200 && "$n" -le 1515024000 ]] && echo "$f"; done
关于 bash 参数扩展的更多细节:${PARAMETER/PATTERN/STRING}
我检查了这个在 bash 4.4.12 和 zsh 5.4.2 中工作的命令行。
关于linux - 过滤文件名中嵌入时间戳的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48428800/