2014-01-16 36 views
0

此腳本檢查是否存在以及修改日期是否小於1天。我一直試圖讓它遍歷整個目錄來尋找我試過的文件,然後找到它,但是我不太熟悉unix腳本。用於搜索文件子目錄的unix腳本

請告知正確的語法或命令是什麼,如果我正在查找的文件位於上傳目錄中,那麼腳本工作正常。

問題 我想搜索上傳目錄下的文件的所有子目錄? :o) 我正在使用mac終端作爲unix環境。

filename=help.csv 

filedir="localhost/upload/" 

cd $filedir 

# if [ find . -name "$filename" ]; then 
if [ -e "$filename" ]; then    # what should I add here to find the file in any subdirectory of upload 

    echo "File Exists" 
    filestr=`find . -name $filename -mtime +1 -print` 
    if [ "$filestr" = "" ]; then 
    echo "File is not older than 1 day" 
    else 
    echo "File is older than 1 day" 
    fi 
else 

    echo "File does not exist" 
fi 

exit 0 

回答

2

您應該只能在上傳目錄中啓動find

find /path/to/upload/dir -name $filename -mtime +1 -print 

.你現在在你的調用只是意味着「開始看的。」,這是當前目錄。

+0

如果[find/localhost/upload/-name「$ filename」-mtime +1 -print];我已經修改了我的腳本。然後我只是得到一個錯誤「太多的論點」任何想法?我的語法是正確的。 –

+0

'find'命令是否從命令行工作?我懷疑你只是在錯誤地構造'find'。 –

+0

我相信Andy建議你用他寫的'find'命令替換你的整個腳本。沒有'if's或'[]'s ...手動運行'find'命令。 – dg99

相關問題