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