Shell script implements recursive delete empty folder _linux Shell

Source: Internet
Author: User

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:

# sh dedr.sh

Deleted files:

Results:

We can see that the empty folder has been deleted.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.