我试图使用shell脚本在修改日期的排序顺序中从目录中查找某些修改日期之后的所有文件。这是我当前shell脚本的一部分。
条件包含两个步骤:1)在某个修改日期之后从目录中获取文件。 2)按修改日期对文件进行排序。
当前目录包含以下文件:
Mar 28 11:14 file_H_1
Apr 2 16:37 file_K_2
Apr 1 21:43 file_H_3
Apr 16 19:16 file_H_4
Apr 16 21:00 file_H_5
Apr 16 12:00 file_L_6
Apr 9 14:08 file_B_7
Apr 4 00:39 file_H_8
Apr 4 00:39 file_H_9
Apr 4 00:39 file_C_10
Apr 4 00:39 file_H_11
Mar 27 14:39 file_H_12
我希望最终输出为“2018-04-09 00:00:00”之后修改日期的文件列表,并按修改日期顺序排列。 输出应该是:
file_B_7
file_L_6
file_H_4
file_H_5
我试过这个:
1)在某个修改日期之后获取文件
find . -type f -newermt "2018-04-09 00:00:00"
OUTPUT of this:
file_L_6
file_H_5
file_B_7
file_H_4
2)在修改日期订购文件
ls -lt
OUTPUT of this
Apr 16 21:00 file_H_5
Apr 16 19:16 file_H_4
Apr 16 12:00 file_L_6
Apr 9 14:08 file_B_7
Apr 4 00:39 file_H_8
Apr 4 00:39 file_H_9
Apr 4 00:39 file_C_10
Apr 4 00:39 file_H_11
Apr 2 16:37 file_K_2
Apr 1 21:43 file_H_3
Mar 28 11:14 file_H_1
Mar 27 14:39 file_H_12
但我正在努力将这两个条件结合起来。
我也尝试了这个,但它是在文件名上排序而不是在修改日期:
find . -type f -newermt "2018-04-9 00:00:00" | sort -n | while read file_name; do
echo file=$file_name
done
OUTPUT of this:
file_B_7
file_H_4
file_H_5
file_L_6
请提出一些解决方案。
答案 0 :(得分:2)
将find
和ls -t
与管道结合使用:
find -type f -newermt "2018-04-09 00:00:00" | xargs ls -tl
xargs
将find
的输出提供给ls
。
如果您的某些文件名包含空格,请使用:
find -type f -newermt "2018-04-09 00:00:00" -print0 | xargs -0 ls -tl
-print0
选项允许使用文件名deleimiter作为\0
。 xargs
期望此分隔符带有-0
选项。
请注意,如果您想要相反的时间顺序,可以使用ls -ltr
。
在任何情况下,您都不应解析ls
(GoogleMap
)的结果。
答案 1 :(得分:1)
您也可以将查找输出直接插入xargs ls
命令行,而不是使用ls
:
ls -tr `find -type f -newermt "2018-04-09 00:00:00"`
请注意,如果并非所有文件名都适合一个命令行 - xargs ls
,则无法正确排序,这两种方法都会失败,此处显示错误消息。
答案 2 :(得分:0)
这应该可以解决问题。
ls -t | egrep "$(find * -maxdepth 0 -type f -newermt "2018-04-9 00:00:00" -print | sed 's/|/\\|/g' | tr '\n' '|' | sed 's/|$//')"
答案 3 :(得分:0)
试试这个..
ls -t | find . -type f -newermt "2018-04-09 00:00:00"