Sometimes we need to delete the empty folder recursively, search the internet for a while, did not find a better shell script, so we wrote a
Script
Copy Code code as follows:
#!/bin/bash
# Author: Ten years later, Lou's brother
# Des:delete Empty Directories recursive
Deleteempty () {
Find ${1:-.}-mindepth 1-maxdepth 1-type D | While Read-r dir
Todo
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's content is very simple, that is, traverse the directory, find an empty folder, and then delete.
Use
If the script file is dedr.sh, the file structure we are testing is:
To run the script:
Copy Code code as follows:
Deleted files:
Results:
We can see that the empty folder has been deleted.