根据unix中的文件名Timestamp查找目录中最旧的文件

时间:2013-04-03 12:02:14

标签: unix sed awk

我希望首先列出基于文件名日期和时间戳的目录中最旧的文件。

示例:

输入文件:

AAAG11020709581.txt
AAAG13020709581.txt
AACL11020709581.txt
AACL13020709581.txt
AAFU11020709581.txt
AAFU13020709581.txt
AAHO11020709581.txt
AAHO13020709581.txt
AAPC11020709581.txt
AAPC13020709581.txt
AAPO11020709581.txt
AAPO13020709581.txt
AATR11020709581.txt
AATR13020709581.txt
AARC11020709581.txt
AARC13020709581.txt

预期产出:

AAAG11020709581.txt
AACL11020709581.txt
AAFU11020709581.txt
AAHO11020709581.txt
AAPC11020709581.txt
AAPO11020709581.txt
AARC11020709581.txt
AATR11020709581.txt
AAAG13020709581.txt
AACL13020709581.txt
AAFU13020709581.txt
AAHO13020709581.txt
AAPC13020709581.txt
AAPO13020709581.txt
AARC13020709581.txt
AATR13020709581.txt

任何人都可以建议吗?

1 个答案:

答案 0 :(得分:5)

默认情况下,排序将以行的开头作为键进行排序。您可以使用-k FIELD.OFFSET表示法告诉它从不同的地方开始,例如如果所有文件名以4个字母开头,您可以跳过这些:

sort -k1.5

输出:

AAAG11020709581.txt
AACL11020709581.txt
AAFU11020709581.txt
AAHO11020709581.txt
AAPC11020709581.txt
AAPO11020709581.txt
AARC11020709581.txt
AATR11020709581.txt
AAAG13020709581.txt
AACL13020709581.txt
AAFU13020709581.txt
AAHO13020709581.txt
AAPC13020709581.txt
AAPO13020709581.txt
AARC13020709581.txt
AATR13020709581.txt