终端使用shell脚本批量删除空文件夹

# !/bin/bash
# 批量删除空文件目录
# 使用方法:(1)进入要操作的目录:cd 目录名;(2)运行脚本:sh 脚本名.sh
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

你可能感兴趣的:(脚本学习)