按修改时间排序文件

时间:2012-05-11 20:17:40

标签: bash sorting

我想使用例如bubblesort对修改时间进行排序,并使用 stat -c%y%n filename 。

 #!/bin/sh

 arr=(*)  # * is list of all file and dir names

 newest=${arr[0]}
 for f in "${arr[@]}";
 do 
    if [ $f -nt $newest ]; then 
    newest=$f  
    fi 
 echo "$(stat -c %y %n $newest)"
 done

我想输出类似 ls -art --full-time 的输出,但当然这更完整。我的问题是为什么它不打印文件名?非常感谢。

1 个答案:

答案 0 :(得分:2)

格式字符串必须是单个参数;使用引号来完成此任务。你可能也不想在那里使用echo $(stat ...),正如我在评论中所指出的那样。

stat -c '%y %n' "$newest"

(从技术上讲,你应该做更多的引用,反对文件名中包含空格的可能性。)