zoukankan      html  css  js  c++  java
  • shell脚本递归删除空文件夹

    有时我们需要递归删除空文件夹,网上找了一下,没有发现比较好的脚本,于是自己动手写了一个

    脚本

    #!/bin/bash
    # author: 十年后的卢哥哥(http://www.cnblogs.com/lurenjiashuo/)
    # 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

    删除的文件:

    结果:

    我们可以看到空文件夹已经被删除了。

    参考文档

    1、finding-empty-directories-unix

  • 相关阅读:
    第二次站立会议6
    第二次冲刺计划会议5
    第二次冲刺计划会议4
    第一次冲刺计划总结
    历史分割
    并查集
    archetype mvn 创建骨架
    protobuf 实战
    grizzly 实战
    RSA 非对称加密
  • 原文地址:https://www.cnblogs.com/lurenjiashuo/p/delete-empty-directories-recursive.html
Copyright © 2011-2022 走看看