如何在按 'last modified' 排序的 shellscript 中使用文件名进行循环?

How to do a loop with filenames in shellscript ordered by 'last modified'?

我是 Linux 的新手,我一直在尝试 运行 使用 ImageMagick 的 convert 处理文件夹中所有文件的脚本(我宁愿这样做Shell 中的任务比使用 mogrify 中的任务要多,因为据我所知它不会保存不同的文件)。这些文件必须按 'last modified' 顺序处理,所以我使用了这个代码:

for file in `ls -1tr {*.jpg,*.png}`; do
    # imagemagick processes with the filename...
done

对于带空格的文件,此代码会中断,根据 this answer,使用 ls 是错误的。

我也尝试了 this response 的这个解决方案,但显然我完全错了(它引发了 'ambiguous redirect' 错误),我决定需要帮助。

while read LINE; do
    ...
done `(ls -1tr {*.png,*.jpg}`

那么如何获得一个循环的有序文件名列表? (它不一定必须是 FOR...IN 循环,它可以是 WHILE 或任何东西。)

试试这个:

for file in `ls -1tr {*.jpg,*.png} | awk '{print }'`; do
    # imagemagick processes with the filename...
done

ls -lrth 在输出中给出 9 个列,其中你只需要第 9 列(文件名),你可以使用 awk

如果您有 space 个单独的文件名,请修改 awk print 以打印第 9 列后的所有数据