Shell脚本实现递归删除空文件夹
有时我们需要递归删除空文件夹,网上找了一下,没有发现比较好的Shell脚本,于是自己动手写了一个
脚本
复制代码 代码如下:
#!/bin/bash
# author: 十年后的卢哥哥
# des: delete empty directories recursive
deleteempty() {
find ${1:-.} -mindepth 1 -maxdepth 1 -type d | while read -r dir
do
if [[ -z "$(find "$dir" -mindepth 1 -type f)" ]] >/dev/null
then
echo "$dir"
rm -rf ${dir} 2>&- && echo "Empty, Deleted!" || echo "Delete error"
fi
if [ -d ${dir} ]
then
deleteempty "$dir"
fi
done
}
deleteempty
脚本的内容很简单,就是遍历目录,找出空文件夹,然后删除。
使用
假如脚本文件为dedr.sh,我们测试的文件结构为:

运行脚本:
复制代码 代码如下:
# sh dedr.sh
删除的文件:

结果:

我们可以看到空文件夹已经被删除了。
您可能感兴趣的文章
- 12-31Shell脚本中获取进程ID的方法
- 12-31Shell脚本美化登录界面装饰图(含农历)
- 12-31Shell 编程:Bash空格的那点事
- 12-31Shell中获取脚本所在目录绝对路径的方法
- 12-31Shell脚本实现乱序排列文件内容的多种方法(洗牌问题)
- 12-31shell 编程中空格的使用方法
- 12-31Shell脚本实现ftok函数
- 12-31getcwd cannot access parent directories错误解决方法
- 12-31Shell脚本实现递归删除空文件夹
- 12-31Shell脚本实现获取网页快照并生成缩略图







