find 除了某个文件之外,删除所有其他文件 (-not -name、-delete)

 

1.
除了 HoloCircleView.java 文件,其他文件全部删除
find . -type f -not -name "HoloCircleView.java" -delete

 

find . -not -name "HoloCircleView.java" -delete

 

除了 HoloCircleView.java 和 current.tx之外,其他文件和目录全部删除
find . -not -name "HoloCircleView.java" -not -name "current.txt" -delete

 

2.
递归删除空目录

 

~/deleteEmptyDir.sh

 

#!/bin/bash
# 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
posted @ 2020-10-26 11:30  行走的思想  阅读(681)  评论(0编辑  收藏  举报