欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Shell腳本實(shí)現(xiàn)遞歸刪除空文件夾

 更新時(shí)間:2015年02月07日 14:47:25   投稿:junjie  
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)遞歸刪除空文件夾,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下

有時(shí)我們需要遞歸刪除空文件夾,網(wǎng)上找了一下,沒有發(fā)現(xiàn)比較好的Shell腳本,于是自己動(dòng)手寫了一個(gè)

腳本

復(fù)制代碼 代碼如下:

#!/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

腳本的內(nèi)容很簡單,就是遍歷目錄,找出空文件夾,然后刪除。

使用

假如腳本文件為dedr.sh,我們測試的文件結(jié)構(gòu)為:

運(yùn)行腳本:

復(fù)制代碼 代碼如下:

# sh dedr.sh

刪除的文件:

結(jié)果:

我們可以看到空文件夾已經(jīng)被刪除了。

相關(guān)文章

最新評論