从最近特定时间修改的子目录中获取所有文件

时间:2013-02-05 21:15:16

标签: bash

我有一个学生提交的项目目录:

+ submissions
|
+-+ Student_Name_1
|
+-+ Student_Name_2
|
+-+ Student_Name_3
|
...

每个子目录可能包含一个或多个tar / zip / tgz / tar.gz / etc.文件。 有些目录可能是空的,因为学生根本没有提交。

项目提交截止日期是在特定的设定时间。什么是最有效的方法(最好是在BASH脚本中,保持简单)只获取在提交截止日期之前创建的最新文件的副本,并将其复制到另一个文件名目录:
 Student_Name_#--original_file_name.tar/zip

2 个答案:

答案 0 :(得分:2)

也许:如果我理解要求找到最接近截止日期的提交。

set -e
touchfile=touchfile
deadline=02010930 #CHANGEME
touch -t $deadline $touchfile
files=`find submissions  -type f  | while read filename ; do
    if [[ $filename -ot $touchfile ]] ; then
        echo $filename
    fi
done`

[[ -n "$files" ]] && {
    thefile=`ls -t $files | head -1`

    [[ -n "$thefile"  ]] && {
        # REMOVE echo when happy :D
        echo cp $thefile $(basename $(dirname $thefile))-$(basename $thefile)
    }
}

答案 1 :(得分:1)

这样的事情应该这样做:

DESTINATION="../final"
for D in `find . -type d`
do
    LASTFILE=`cd ${D};ls -art1 *.{zip,tar} | tail -1`
    #note that the last arg of each call is the number one, not the letter 'L'
    cp ${D}/${LASTFILE} ${DESTINATION}/${D}-${LASTFILE}
done