首页 > 解决方案 > 获取文件的最后修改日期

问题描述

我正在编写一个 bash 程序来按修改日期的顺序对来自不同文件夹的照片进行排序。我知道如何列出所有文件的名称,但我怎样才能获得它们的修改日期呢?

Matthews-MacBook-Air-3:Q3 Matthew$ find . -name '*.jpg'

./MontrealTest/daves_images/mtl10.jpg
./MontrealTest/daves_images/mtl7.jpg
./MontrealTest/gregs_photos/mtl1.jpg
./MontrealTest/photos_by_harth/mtl11.jpg
./MontrealTest/photos_by_harth/mtl5.jpg
./MontrealTest/photos_by_harth/mtl9.jpg
./MontrealTest/sandeeps_collection/mtl4.jpg
./MontrealTest/sandeeps_collection/mtl8.jpg
./MontrealTest.jpg
./SimpleTest/dir1/fee.jpg
./SimpleTest/dir2/fum.jpg
./SimpleTest/dir3/foo.jpg
./SimpleTest/dir4/foe.jpg
Matthews-MacBook-Air-3:Q3 Matthew$ 

标签: bashshellsortingcommand-line

解决方案


这取决于find您拥有的版本(通常取决于您使用的操作系统)。如果find支持-printf原语,您可以执行以下操作(但请参阅man find更多格式选项):

find . -name '*.jpg' -printf '%t %p\n'

如果您find不支持-printf,您可以让它执行stat以执行相同的操作(同样,请参阅man stat更多格式选项):

find . -name '*.jpg' -exec stat -f '%Sm %N' {} +

推荐阅读