在bash脚本中,如何循环浏览按日期排序的文件

时间:2019-03-19 03:24:13

标签: bash sorting

我有以下脚本:

#!/bin/sh
HOST='host'
USER='user@example.com'
PASSWD='pwd'

for FILE in *.avi
do
    ftp -n $HOST <<END_SCRIPT
    quote USER $USER
    quote PASS $PASSWD
    binary
    put $FILE
    rm FILE 
    quit
done
END_SCRIPT
exit 0

如何确保首先处理最早的文件?我已经看到很多使用ls -t的技巧,但是必须有一种更简单的方法。

1 个答案:

答案 0 :(得分:0)

我会用以下方法修改您的for循环:

for FILE in `ls -tU *.avi`
do
  #
  # loop content here with ${FILE}
  #
done

来自ls文档:

-t      Sort by time modified (most recently modified first) before 
        sorting the operands by lexicographical order.
-U      Use time of file creation, instead of last modification for 
        sorting (-t) or long output (-l).
相关问题