#!/bin/bash //声明使用的shell
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin/;/usr/local/sbin:~/bin
export PATH
read -p "Please input your directory name:" d_name //d_name让你输入目录名
if[ "$d_name" == "" ] || [ ! -d "$d_name" ];then //没输入,或者不是目录,退出
echo -e "The director is not exist,please check it"
exit 1
fi
filelist=`ls $d_name` //filelist 为该目录下的所有文件,包括普通文件和目录文件
for filename int $filelist //shell for 循环
do
if[ -f $filename ];then //是文件,删除,不是,继续循环,到结束
rm -rf $filename
fi
done
假设脚本名为 del.sh,内容如下:
#!/bin/bash
deleteFile() {
for file in *
do
line=`ls -l $file`
[ ${line:0:1} = "-" ] && rm -f $file
done
}
cd $1
[ $? -ne 0 ] && exit 1
deleteFile
echo "Delete operation finished!"
运行脚本:
del.sh “你的目录"