2014-01-08 198 views
0

我試圖基本上看一個具有每個用戶的子目錄的目錄。說user1和user2等等。我想基本瀏覽每個目錄並查找比特定日期早的文件並記錄下來。我可以通過ls -d/directory/* /獲得目錄列表。我知道我可以使用find ./ -type f -mtime +30來查找未修改超過30天的文件。我無法弄清楚如何使用find,因此每次都會查找./user1 -type f -mtime30,然後是./user2等等。循環的bash腳本

#!/bin/bash 
LIST="$(ls -d /Volumes/db_backups/*/)" 
for i in "$LIST";do 
     fold=`basename $i` 
     echo $fold 
     ModList=$(find $i -type f -mtime +30 >$fold.out) 
     $ModList 
done 

更新:我能夠得到一個文件列表。但是,我不想循環訪問特定的子目錄並向每個用戶發送電子郵件。例如,我有一個文件被記錄爲/dir1/dir2/user1/file.txt。我將如何獲取user1列表並將其放入user1.out然後user2.out等等?

+0

你想通過其中的路徑的子字符串拆分文件嗎? – l0b0

+0

我想我正在接近。我將它添加到我的for循環中。 我在「$ LIST」;做 fold ='basename $ i' 所以基本上我想爲每個具有超過x天的文件列表的子目錄創建輸出文件。然後我想我基本上可以將每個輸出文件發送給每個用戶。 – philfry

+0

請將您的實際代碼添加到問題中。 – l0b0

回答

0

如果你真的想運行每個子目錄一個單獨的進程,那麼這就是你想要的東西:

cd /Volumes/db_backups 
for entry in * 
do 
    if [[ -d "${entry}" ]]      # skip things that aren't directories 
    then 
    find "${entry}" -type f -mtime +30 -print > /some/log/directory/"${entry}".output 
    fi 
done 

if ... fi位可以縮短爲[[ -d "${entry}" ]] && find ...,這是更簡潔,但不是很明確有關你的意圖。

1

您可以提供多個路徑find

find directory/* -type f ... 

這裏directory/*將擴展爲directory/user1 directory/user2,實際上給多個路徑find

編輯關於第二個想法,你應該可以使用find directory -type f(帽子提示@anubhava)。

+0

這與'find directory -type f'有什麼不同? – anubhava

+0

@anubhava:好點。它可能不是。 – NPE