2017-03-29 7 views
1

さまざまなフォルダのディレクトリ構造を使用しています。いくつかのファイルに毎日新しいファイルが作成されています。シェルスクリプトを使用して効率的にフォルダをクリーンアップする方法

ディレクトリをクリーンアップするプログラムをいくつか作成しましたが、シェルスクリプトを使用して効率的に使用したいと考えています。

したがって、「archiving.properties」ファイルを、すべてのフォルダをクリーンアップする必要があります。

  1. は、すべてのプロパティを見つける
  2. は、ファイル名のパターン(file_pattern)と一致しているすべてのファイルを削除するファイル:プロパティファイルには、必要がある私のクリーンアップルーチンは今、次の変数

    file_pattern="*.xml" 
    days_to_keep=2 
    

    が含まれている必要がありますプロパティファイルが見つかったディレクトリの定義された日数(days_to_keep)より古い。

私の質問はどのようにして最も効果的な方法でこれを行うことができるのですか?

find . -type f -name "archiving.properties" -print 
find . -type f -name "<file_pattern>" -mtime +<days_to_keep> -delete 

現在、私は以下のフォルダを1つのフォルダに入れようとしていました。コマンドを正しく出力しますが、実行されません。

#!/bin/bash 
. archiving.properties 
find . -type f -name "*.xml" -mtime +1 -exec rm -rf {} \; 
echo " find . -type f -name \"${file_pattern}\" -mtime +${days_to_keep} -exec rm -rf {} \;" 


Result is: find . -type f -name "*.xml" -mtime +1 -exec rm -rf {} \; 

ありがとうございました。

+0

を得ましたか。ディレクトリツリー全体を操作するのは忘れてください。あなたが提案した 'archive.properties'ファイルを含む単一のディレクトリを持っているなら、あなたはそれをどのように扱うのか考えましたか? – larsks

答えて

0

私はあなたがこれまでに試してみました何最終結果

echo "start deleting files in " $(pwd) " ... " 

#filename of the properties 
properties="clean_up.properties" 

#find all properties files 
for prop in $(find . -type f -name $properties);do 
     #init variables 
     file_pattern="*._html" 
     days_to_keep=14 

     #load the variables from the properties file 
     . "$prop" 

     #define the folder of the properties file 
     folder=${prop%?$properties} 

     #remove all files matching the parameters in the folder where the properties were found 
     echo ">>> find $folder -type f -name \"${file_pattern}\" -mtime +${days_to_keep} -exec rm -f {} \;" 
        find $folder -type f -name "${file_pattern}" -mtime +${days_to_keep} -exec rm -f {} \; 
done 
echo "... done" 
関連する問題