易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
首页
/
操作系统
/
Linux
/
Linux下使用Shell脚本删除一个目录下的所有子目录和文件
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
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
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图