說明
此腳本用於清除特定目錄下的文件,不刪除子目錄。
dir_list
表示目錄列表,temp_dir
表示臨時目錄。
1. 如果dir
存在,且不爲空,則刪除該目錄下所有文件(不刪除子目錄);
2. 再判斷是否存在臨時目錄,如果臨時目錄不爲空,則刪除臨時目錄中的文件。
腳本:
#!/bin/bash
# clean some direcotrys
dir_list=(
/app/billapp/log/chf/chf_005
/app/billapp/log/chf/chf_006
/app/billapp/log/chf/chf_007
/app/billapp/log/chf/chf_008
/app/billapp/data/sg_005_1/ok
/app/billapp/data/sg_005_1/err
/app/billapp/data/cg/ok
/app/billapp/data/cg/err
/app/billapp/data/rf_005/ok
/app/billapp/data/rf_005/err
/app/billapp/data/rf_006/ok
/app/billapp/data/rf_006/err
/app/billapp/data/rf_007/ok
/app/billapp/data/rf_007/err
/app/billapp/data/rf_008/ok
/app/billapp/data/rf_008/err
)
# tmp_top
temp_dir="/tmp_top"
for dir in ${dir_list[@]}
do
# if dir exists and is not empty, then clean it
if [ -d $dir ] && [ "`ls -A $dir`" != "" ]
then
# remove files
echo "rm $dir/*"
rm $dir/*
# if tmp_dir exists and is not empty, then clean it
if [ -d $dir$temp_dir ] && [ "`ls -A $dir$temp_dir`" != "" ]
then
echo "find $dir$temp_dir -type f -exec rm {} \;"
find $dir$temp_dir -type f -exec rm {} \;
fi
else
echo "$dir dont't exist or is empty."
fi
echo
done
exit 0