2013-10-28 36 views
-1

我試圖循環通過一組文件夾,如果一個(或多個)文件比特定的日期更新,它應該顯示該文件夾和文件(s),就像這樣;很好的方式循環導演和文件

(日期是周齡)

Folder 1 
    file 1 
*Folder 2 (no files matching the date, should not be visible/echo'ed)* 
Folder 3 
    file 1 
    file 2 
Folder 4 
    file 1 

如何實現這一目標?我的示例代碼:

#!/opt/bin/bash 
# 
# CurrentTvList.sh 
# File for generating a current list of stored tvshows on my nas 
# Patrick Osterlund 
# 

#set -x 
#trap read debug 

# Settings 
# 
NEWTMPFOLDER="/volume1/tmp/tvtemps/" 
NEWTMPFILE="TmpTvlisting.txt" 
SIMPLER=$NEWTMPFOLDER$NEWTMPFILE 
SMALL=2 
MEDIUM=14 
LARGE=30 

# Clear screen; remove later 
clear 

#check if dir exists 
mkdir -p ${NEWTMPFOLDER} 

#check if file exists 
if [ -f $SIMPLER ]; 
then 
    echo "File: $NEWTMPFILE exists in $NEWTMPFOLDER" $'\n' 

    FILEDATE=$(date -r $SIMPLER) 
    #Check if file is newer than the existing one 
    if [[ $SIMPLER -nt $FILEDATE ]]; then 
     echo "$SIMPLER is newer than $FILEDATE" 
    else 
     echo "file created $FILEDATE is newer than file created; $SIMPLER" 
    fi 
    echo "and it was created: $FILEDATE" #$'\n\n' 
    echo "File was created: " > $SIMPLER 
    echo $FILEDATE >> $SIMPLER $'\n' 
else 
    echo "File $NEWTMPFILE does not exists in $NEWTMPFOLDER, creating it" 
    touch $SIMPLER; 
fi 
# Mostly for checking if all went well: 
echo "looking here:" $NEWTMPFOLDER; 
echo "file: $NEWTMPFILE" 

# Small list 
echo "New shows last ${SMALL} days: " $'\n' >> $SIMPLER 
find /volume2/Disk2/!Tv-Serier/ -type f -mtime -$SMALL -size +2048 -exec basename {} \; | sed 's/\.[^.]*$//'\ 
>> $SIMPLER 

# Medium list 
echo $'\n' "New shows last ${MEDIUM} days: " $'\n' >> $SIMPLER 
find /volume2/Disk2/!Tv-Serier/ -type f -mtime -$MEDIUM -size +2048\ 
-exec basename {} \; | sed 's/\.[^.]*$//' >> $SIMPLER | sort -df 

# Not in use right now 
# Large list 
#echo $'\n' "New shows last ${LARGE} days: " $'\n' >> $SIMPLER 
#find /volume2/Disk2/!Tv-Serier/ -type f -mtime -$LARGE -size +2048\ 
# -exec basename {} \; | sed 's/\.[^.]*$//' >> $SIMPLER | sort -df 

回答

0

您可以將-newerct選項find中進行了修改,在上週,這樣當前目錄列出文件:

find . -maxdepth 1 -type f -newerct '1 week ago' 

這裏-maxdepth 1限制搜索當前目錄; -type f只選擇純文本文件(不是子目錄等),並在-newerctc的意思是「inode修改時間」和t的意思是「解釋一個參數爲形式的直接日期說明通過cvs瞭解」

所以,你的腳本看起來是這樣的:

something | while read DIRECTORY; do { 
    cd -- "$DIRECTORY" 
    if find . -maxdepth 1 -type f -newerct '1 week ago' | grep -q . 
    then 
     printf "%s\n" "$DIRECTORY" 
     printf " %s\n" * 
    fi 
} done 

哪裏something是列出要通過搜索目錄的命令。

我假設在上面的代碼中,你的意思,你在問題中寫道:那就是,如果任何文件目錄中的較新,只有一個星期的,那麼你要列出所有文件在目錄中。

+0

newerct在busybox中不起作用,顯然是:/ 但是不,我只想列出較新的文件,舊的文件對此目的不感興趣。 – b0red

+0

這是一種有助於你在你的問題中提及的細節,因爲「[BusyBox中的實用程序通常比全功能的GNU表兄弟選項更少](http://www.busybox.net/about html的)。」同樣,如果您只希望列出較新的文件,您應該提到這一點。 –

+0

對不起,我對這個論壇有點新,將來會試着提供更多信息:/ – b0red