Linux 腳本 —— 刪除特定目錄中的文件


說明

此腳本用於清除特定目錄下的文件,不刪除子目錄。

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章