Shell scripts recursively Delete empty folders
Sometimes we need to recursively Delete empty folders. After searching for them online, we did not find a good Shell script, So we wrote
Script
#! /Bin/bash # author: a decade later, brother Lu (http://www.bkjia.com/) # des: delete empty directories recursivedeleteempty () {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
The script content is very simple, that is, traverse the directory, find the empty folder, and then delete it.
Use
If the script file is dedr. sh, the structure of the file we tested is:
Run the script:
# Sh dedr. sh
Deleted files:
Result:
We can see that the empty folder has been deleted.
This article permanently updates the link address: