Linux下使用Shell腳本刪除一個目錄下的所有子目錄和文件

 

#!/bin/sh         
#FileName:deleteDir.sh      
#Function:Linux下使用Shell腳本刪除一個目錄下的所有子目錄和文件(不可恢復刪除且目錄下目錄名和文件名中沒有空格)       
#Version:V0.1         
#Author:Sunrier         
#Date:2012-08-01 

CURRPATH=$PWD

#DESTPATH爲刪除的目標目錄(目標目錄本身不會刪除)
DESTPATH="/home/Sunrier/Trash"

#isNullDir函數判斷進入的目錄下是否存在下級子目錄或者文件
#存在返回1,不存在返回0
isNullDir()
{
	local NewCurrentPath=$PWD
	local NewDirName=$1
	cd $NewDirName
	local NewFileList=`ls 2>/dev/null` 
	
#	-n string :	如果字符串不爲空,則結果爲真	

	if [ -n "$NewFileList" ]
	then
		echo "目錄$NewDirName下列表信息爲$NewFileList"
		cd $NewCurrentPath
		return 1 
	else
		echo "目錄$NewDirName爲空目錄"
		cd $NewCurrentPath
		return 0	
	fi	
}

if [ "Sunrier" != "$LOGNAME" ]
then
	echo "您沒有執行權限!請聯繫管理員!"
	exit 1	
fi

cd $DESTPATH 2>/dev/null

if [ $? -ne 0 ]
then
	echo "沒有找到目標目錄!"
	exit 1
fi

echo "您正準備刪除的目錄爲$DESTPATH"

#不考慮隱藏目錄和文件
FileList=`ls 2>/dev/null`

while [ "" != "$FileList" ]
do
	echo "當前列表信息爲 $FileList"
	
	for pFile in $FileList
	do
		echo "加載 ${pFile} 中......"
		if [ -d "${pFile}" ]
		then
			echo "檢查到 ${pFile} 爲目錄 "
			echo "目錄 ${pFile} 處理中......"
			
#注:在Linux的Shell腳本中,調用函數作爲判斷條件時,函數返回值爲0時,if後的條件才爲真;否則if後的條件爲假!				
			if  isNullDir "${pFile}"
			then
				echo  "刪除目錄 ${pFile} 中......"
				rm -rf ${pFile}
			else
				echo "移動目錄${pFile}下的所有目錄和文件中......"
				mv ${pFile}/* . >/dev/null 2>&1
				if [ $? -ne 0 ]
				then
					ModifyTime=`date +%Y%m%d%H%M%S`  
					mv ${pFile} $ModifyTime 
				fi	
			fi
		else
			echo "檢查到 ${pFile} 爲文件 "
			echo "正在把文件 ${pFile} 的文件名更改爲文件名 1 中......"
			mv ${pFile} 1 2>/dev/null
		fi

		if [ -f 1 ]
		then
			echo "發現文件名爲1的文件,正在刪除文件1中......"
			echo "123456"> 1
			rm -rf 1		
		fi
	done
	
	echo "更新新的文件列表中......"
	cd $DESTPATH
	FileList=`ls 2>/dev/null`
	
done				

if [ $? -eq 0 ]
then
	echo "清理成功!"
	cd $CURRPATH
	exit 0
else
	echo "清理失敗!"
	cd $CURRPATH
	exit 1
fi




 

 

注:去掉一些顯示信息


#!/bin/sh         
#FileName:deleteDir.sh      
#Function:Linux下使用Shell腳本刪除一個目錄下的所有子目錄和文件(不可恢復刪除且目錄下目錄名和文件名中沒有空格)       
#Version:V0.2        
#Author:Sunrier         
#Date:2012-08-27 

CURRPATH=$PWD

#DESTPATH爲刪除的目標目錄(目標目錄本身不會刪除)
DESTPATH="/home/Sunrier/Trash"


#isNullDir函數判斷進入的目錄下是否存在下級子目錄或者文件
#存在返回1,不存在返回0
isNullDir()
{
	local NewCurrentPath=$PWD
	local NewDirName=$1
	cd $NewDirName
	local NewFileList=`ls 2>/dev/null` 
	
#	-n string :	如果字符串不爲空,則結果爲真	

	if [ -n "$NewFileList" ]
	then
		cd $NewCurrentPath
		return 1 
	else
		cd $NewCurrentPath
		return 0	
	fi	
}

if [ "Sunrier" != "$LOGNAME" ]
then
	echo "您沒有執行權限!請聯繫管理員!"
	exit 1	
fi

cd $DESTPATH 2>/dev/null

if [ $? -ne 0 ]
then
	echo "沒有找到目標目錄!"
	exit 1
fi

echo "您正準備刪除的目錄爲$DESTPATH"

#不考慮隱藏目錄和文件
FileList=`ls 2>/dev/null`

while [ "" != "$FileList" ]
do
	
	for pFile in $FileList
	do
		echo "加載 ${pFile} 中......"
		if [ -d "${pFile}" ]
		then
			
#注:在Linux的Shell腳本中,調用函數作爲判斷條件時,函數返回值爲0時,if後的條件才爲真;否則if後的條件爲假!				
			if  isNullDir "${pFile}"
			then
				rm -rf ${pFile}
			else
				mv ${pFile}/* . >/dev/null 2>&1
				if [ $? -ne 0 ]  
				then  
					ModifyTime=`date +%Y%m%d%H%M%S`    
					mv ${pFile} $ModifyTime   
				fi    
			fi
		else
			mv ${pFile} 1 2>/dev/null
		fi

		if [ -f 1 ]
		then
			echo "123456">1
			rm -rf 1		
		fi
	done
	
	cd $DESTPATH
	FileList=`ls 2>/dev/null`
	
done				

if [ $? -eq 0 ]
then
	echo "清理成功!"
	cd $CURRPATH
	exit 0
else
	echo "清理失敗!"
	cd $CURRPATH
	exit 1
fi 




 

//解決目錄下目錄名和文件名中含有空格的情況


#!/bin/sh         
#FileName:deleteDir.sh      
#Function:Linux下使用Shell腳本刪除一個目錄下的所有子目錄和文件(不可恢復刪除)       
#Version:V0.3         
#Author:Sunrier         
#Date:2012-08-29 

CURRPATH=$PWD

#DESTPATH爲刪除的目標目錄(目標目錄本身不會刪除)
DESTPATH="/home/Sunrier/Trash"

#isNullDir函數判斷進入的目錄下是否存在下級子目錄或者文件
#存在返回1,不存在返回0
isNullDir()
{
	local NewCurrentPath=$PWD
	local NewDirName=$1
	cd "$NewDirName"
	local NewFileList=`ls 2>/dev/null` 
	
#	-n string :	如果字符串不爲空,則結果爲真	

	if [ -n "$NewFileList" ]
	then
		echo "目錄$NewDirName下列表信息爲$NewFileList"
		cd $NewCurrentPath
		return 1 
	else
		echo "目錄$NewDirName爲空目錄"
		cd $NewCurrentPath
		return 0	
	fi	
}

if [ "Sunrier" != "$LOGNAME" ]
then
	echo "您沒有執行權限!請聯繫管理員!"
	exit 1	
fi

cd $DESTPATH 2>/dev/null

if [ $? -ne 0 ]
then
	echo "沒有找到目標目錄!"
	exit 1
fi

echo "您正準備刪除的目錄爲$DESTPATH"

#不考慮隱藏目錄和文件
#FileList=`ls 2>/dev/null`
FileList=`ls | tr " " "?" 2>/dev/null`

while [ "" != "$FileList" ]
do
	echo "當前列表信息爲 $FileList"
	
	for pFile in $FileList
	do
		echo "加載 ${pFile} 中......"
		if [ -d "${pFile}" ]
		then
			echo "檢查到 ${pFile} 爲目錄 "
			echo "目錄 ${pFile} 處理中......"
			
#注:在Linux的Shell腳本中,調用函數作爲判斷條件時,函數返回值爲0時,if後的條件才爲真;否則if後的條件爲假!				
			if  isNullDir "${pFile}"
			then
				echo  "刪除目錄 ${pFile} 中......"
				rm -rf "${pFile}"
			else
				echo "移動目錄${pFile}下的所有目錄和文件中......"
				mv "${pFile}"/* . >/dev/null 2>&1
				if [ $? -ne 0 ]
				then
					echo "發現父目錄與子目錄同名,試圖更改父目錄目錄名......"
					ModifyTime=`date +%Y%m%d%H%M%S`  
					mv "${pFile}" $ModifyTime 
				fi	
			fi
		else
			echo "檢查到 ${pFile} 爲文件 "
			echo "正在把文件 ${pFile} 的文件名更改爲文件名 1 中......"
			mv "${pFile}" 1 2>/dev/null
		fi

		if [ -f 1 ]
		then
			echo "發現文件名爲1的文件,正在刪除文件1中......"
			echo "123456" > 1
			rm -rf 1		
		fi
	done
	
	echo "更新新的文件列表中......"
	cd $DESTPATH
	#FileList=`ls 2>/dev/null`
	FileList=`ls | tr " " "?" 2>/dev/null`
	
done				

if [ $? -eq 0 ]
then
	echo "清理成功!"
	cd $CURRPATH
	exit 0
else
	echo "清理失敗!"
	cd $CURRPATH
	exit 1
fi




 

 

//去掉一些顯示信息



#!/bin/sh         
#FileName:deleteDir.sh      
#Function:Linux下使用Shell腳本刪除一個目錄下的所有子目錄和文件(不可恢復刪除)       
#Version:V0.4        
#Author:Sunrier         
#Date:2012-08-29 

CURRPATH=$PWD

#DESTPATH爲刪除的目標目錄(目標目錄本身不會刪除)
DESTPATH="/home/Sunrier/Trash"

#isNullDir函數判斷進入的目錄下是否存在下級子目錄或者文件
#存在返回1,不存在返回0
isNullDir()
{
	local NewCurrentPath=$PWD
	local NewDirName=$1
	cd "$NewDirName"
	local NewFileList=`ls 2>/dev/null` 
	
#	-n string :	如果字符串不爲空,則結果爲真	

	if [ -n "$NewFileList" ]
	then
		cd $NewCurrentPath
		return 1 
	else
		cd $NewCurrentPath
		return 0	
	fi	
}

if [ "Sunrier" != "$LOGNAME" ]
then
	echo "您沒有執行權限!請聯繫管理員!"
	exit 1	
fi

cd $DESTPATH 2>/dev/null

if [ $? -ne 0 ]
then
	echo "沒有找到目標目錄!"
	exit 1
fi

echo "您正準備刪除的目錄爲$DESTPATH"

#不考慮隱藏目錄和文件
FileList=`ls | tr " " "?" 2>/dev/null`

while [ "" != "$FileList" ]
do
	
	for pFile in $FileList
	do
		echo "加載 ${pFile} 中......"
		if [ -d "${pFile}" ]
		then
			
#注:在Linux的Shell腳本中,調用函數作爲判斷條件時,函數返回值爲0時,if後的條件才爲真;否則if後的條件爲假!				
			if  isNullDir "${pFile}"
			then
				rm -rf "${pFile}"
			else
				mv "${pFile}"/* . >/dev/null 2>&1
				if [ $? -ne 0 ]
				then
					ModifyTime=`date +%Y%m%d%H%M%S`  
					mv "${pFile}" $ModifyTime 
				fi	
			fi
		else
			mv "${pFile}" 1 2>/dev/null
		fi

		if [ -f 1 ]
		then
			echo "123456" > 1
			rm -rf 1		
		fi
	done
	
	cd $DESTPATH
	FileList=`ls | tr " " "?" 2>/dev/null`
	
done				

if [ $? -eq 0 ]
then
	echo "清理成功!"
	cd $CURRPATH
	exit 0
else
	echo "清理失敗!"
	cd $CURRPATH
	exit 1
fi





 

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