Rad*_*dek 8 ls find shell-script timestamps
我只想在目录 /tmp 中列出所有格式比时间戳更新的文件(按日期排序)20130207003851
。子目录可以省略。
使用 SUSE Linux Enterprise Server 11。
输出格式应该是
S0002948.LOG Feb 7 03:28
S0002935.LOG Feb 7 05:58
S0002952.LOG Feb 7 09:58
S0002940.LOG Feb 7 11:58
Run Code Online (Sandbox Code Playgroud)
find /tmp -newermt "7 feb 2013" -ls列出了我想要的文件,但是
ps 我不想使用 touch 创建用于查找的参考文件
pss
find -newermt "20130207003851" -ls
find: I cannot figure out how to interpret `20130207003851' as a date or time
Run Code Online (Sandbox Code Playgroud)
Gil*_*il' 11
find
支持很多日期输入格式。获取的最简单格式是 YYYYMMDD HH:MM:SS。您已经拥有正确顺序的数字,您所要做的就是提取第一组(${timestamp%??????}
:除最后 6 个字符之外的${timestamp#????????}
所有字符;:除前 8 个字符之外的所有字符),然后继续,附加标点符号,然后是下一组随着你的进行。
timestamp=20130207003851
timestring=${timestamp%??????}; timestamp=${timestamp#????????}
timestring="$timestring ${timestamp%????}"; timestamp=${timestamp#??}
timestring="$timestring:${timestamp%??}:${timestamp#??}"
Run Code Online (Sandbox Code Playgroud)
在 bash(以及 ksh 和 zsh)中,但不是在 ash 中,您可以使用更具可读性的${STRING_VARIABLE:OFFSET:LENGTH}
构造。
timestring="${timestamp:0:8} ${timestamp:8:2}:${timestamp:10:2}:${timestamp:12:2}"
Run Code Online (Sandbox Code Playgroud)
要按日期对文件进行排序,请打印出日期前面的文件名并对其进行排序,然后去掉日期前缀。使用-printf
控制输出格式。打印由 确定的部分修改时间;如果是,您将获得自 Unix 纪元以来的秒数。下面我打印三个制表符分隔的列:可排序格式的时间、文件名和人类可读格式的时间;删除第一列并调用以足够的空格替换制表符以容纳所有预期的文件名(根据需要调整 40)。此代码假定文件名中没有换行符或制表符。%TX
X
X
@
cut -f 2-
expand
find -maxdepth 1 -type f \
-newermt "$timestring" -printf '%T@\t%f\t%Tb %Td %TH:%TM\n' |
sort -k1n |
cut -f 2- |
expand -t 40
Run Code Online (Sandbox Code Playgroud)
与zsh
:
zmodload zsh/stat
newer() {
local t
zstat -A t -F %Y%m%d%H%M%S +mtime -- $REPLY && (( t >= timestamp ))
}
timestamp=20130207003851
print -rl -- /tmp/*(D.Om+newer)
Run Code Online (Sandbox Code Playgroud)
该newer
函数对于更新的文件返回 true $timestamp
(请注意,虽然zsh
'szstat
不支持亚秒精度)。
然后我们所做的就是使用zsh
globbing 限定符:D
包含点文件,.
只考虑常规文件,Om
对修改时间进行排序,并+newer
调用newer
函数来确定选择哪个文件。
如果您还想显示文件时间戳,您可以更改newer
为:
newer() {
local t
zstat -A t -F %Y%m%d%H%M%S +mtime -- $REPLY &&
REPLY+=$'\t'$t &&
(( t >= timestamp ))
}
Run Code Online (Sandbox Code Playgroud)
请注意,文件名生成的结果将包括时间戳,这意味着您不能再将它们用作文件名。
归档时间: |
|
查看次数: |
24978 次 |
最近记录: |