您现在的位置是:网站首页> 编程资料编程资料
Shell脚本实现递归删除空文件夹_linux shell_
2023-05-26
334人已围观
简介 Shell脚本实现递归删除空文件夹_linux 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
删除的文件:

结果:

我们可以看到空文件夹已经被删除了。
您可能感兴趣的文章:
相关内容
- Shell脚本中非交互式修改密码的两种方法_linux shell_
- Shell脚本实现自动安装zookeeper_linux shell_
- Shell脚本实现在Linux系统中自动安装JDK_linux shell_
- Shell脚本实现ftok函数_linux shell_
- Shell实现的Oracle启动脚本分享_linux shell_
- Shell脚本实现查杀子进程、僵尸进程_linux shell_
- Shell脚本实现乱序排列文件内容的多种方法(洗牌问题)_linux shell_
- Shell脚本实现生成SSL自签署证书_linux shell_
- getcwd cannot access parent directories错误解决方法_linux shell_
- Shell编程 Bash引号的那点事_linux shell_
