使用Unix删除超过7天的文件

时间:2013-12-19 11:58:45

标签: shell unix while-loop

要求:

我有文件存在的路径。

我需要从中获取路径并删除超过7天的文件,名称为 .logo ,out0 ..

问题:尝试过以下但是它会进入许多未列出的路径。

 #reading source path from rem_logs.txt

cat rem_logs.txt | while read FILE_PATH
 do
   echo " Path obtained from rem_logs.txt --> '$FILE_PATH'"


   echo "File has to be removed from '$FILE_PATH'"

   #moving to the specified path above
        find  $FILE_PATH -type f -mtime +7 -print | while read FILE_NAME

   echo "File is '$FILE_NAME'"
      do
          chmod 777 $FILE_NAME


         echo "$FILE_NAME is received"
              if [ "$FILE_NAME"=*.log0* -o "$FILE_NAME"=*.out0*] 
                 then 
                     echo " $FILE_PATH/$FILE_NAME" > $LOGPATH/abdul.txt

上面用于测试环境中的测试语句

                 else 
                     echo "This file - $FILE_NAME need not be removed"
              fi
        done
        UpdateLog_del.sh "$FILE_NAME is presently deleted from the above mentioned path"   

done

2 个答案:

答案 0 :(得分:3)

考虑做这样的事情:

while read FILE_PATH
 do
    #for each filename found
    for FILE_NAME in $(find  $FILE_PATH \( -name "*.log0" -o -name "*.out0" \) -type f  -mtime +7 -print)
      do
         chmod 777 $FILE_NAME
         echo "$FILE_NAME" >> $LOGPATH/abdul.txt
      done
      UpdateLog_del.sh "$FILE_NAME is presently deleted from the above mentioned path"
#read from rem_logs.txt which contains the paths
done < rem_logs.txt

答案 1 :(得分:1)

试试这个:

find /path -type f -mtime +7 -regex '$\|.*log0$\|.*out0$' -print | xargs -I '{}' -n1 rm -f {}