Linux 如何使用shell脚本高效地清理文件夹
我正在使用一个目录结构与各种文件夹。其中一些文件每天都会创建新文件 我已经创建了一些程序来清理目录,但是我想使用shell脚本来提高效率 因此,我想在每个需要清理的文件夹中存储一个“存档.properties”文件。属性文件应包含以下变量Linux 如何使用shell脚本高效地清理文件夹,linux,shell,scripting,Linux,Shell,Scripting,我正在使用一个目录结构与各种文件夹。其中一些文件每天都会创建新文件 我已经创建了一些程序来清理目录,但是我想使用shell脚本来提高效率 因此,我想在每个需要清理的文件夹中存储一个“存档.properties”文件。属性文件应包含以下变量 file_pattern="*.xml" days_to_keep=2 现在我的清理程序应该是: 查找所有属性文件 删除与文件名模式(file_模式)匹配的所有文件,以及在找到属性文件的目录中比定义的天数(days_to_keep)更早的文件 所以我的问题是
file_pattern="*.xml"
days_to_keep=2
现在我的清理程序应该是:
find . -type f -name "archiving.properties" -print
find . -type f -name "<file_pattern>" -mtime +<days_to_keep> -delete
提前感谢您的帮助。我得到了最终结果
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"
我得到了最后的结果
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"
到目前为止你试过什么?暂时忘记操作整个目录树;如果您有一个包含您建议的
archive.properties
文件的目录,您是否考虑过如何处理它?到目前为止您尝试了什么?暂时忘记操作整个目录树;如果您有一个包含您建议的archive.properties
文件的目录,您是否考虑过如何处理它?