Shell scripts recursively Delete empty folders

Source: Internet
Author: User

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:

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.