Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8592721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:49:02+00:00 2026-06-11T23:49:02+00:00

Hi I am trying to remove/delete a directory along with its sub directories and

  • 0

Hi I am trying to remove/delete a directory along with its sub directories and files recursively. I don’t want to use rm -r. I have come up with the following code.

 function recursive(){
            for i in "$1"/*; do

                    if [ -d $i ];then
                            echo "FILE $i IS A DIRECTORY"
                            if [ "$(ls -A $i)" ];then
                                    echo "DIRECTORY IS NOT EMPTY CALLING RECURSIVE AGAIN"
                                    recursive $i
                            else
                                    echo "DELETE THIS DIRECTORY: ITS EMPTY"
                            fi
                    elif [ -e $i ];then
                            echo "DELETING FILE $i"
                    else
                            echo UNKNOWN FILE $(basename $i)
                    fi
            done

    }

The problem is as I dig deep into sub directories, I can delete their files on the way, but once I reach the bottom of directory tree, I have to delete all the directories which are now empty (maybe on my way back up?)

Would really appreciate if someone could help me with its logic or guide me in the right direction.

The answer to THIS question makes sense but I don’t know how can it handle if there are few levels of sub directories ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:49:03+00:00Added an answer on June 11, 2026 at 11:49 pm

    After you have deleted the contents of a directory you can delete it in any case. So just remove the else statement and move the remove-directory-command one level up:

    function recursive(){
            for i in "$1"/*; do
    
                    if [ -d $i ];then
                            echo "FILE $i IS A DIRECTORY OR A (SYM)LINK TO ONE"
                            if [ "$(ls -A $i)" ];then
                                    echo "DIRECTORY IS NOT EMPTY CALLING RECURSIVE AGAIN"
                                    recursive $i
                            fi
    
                            echo "DELETE THIS DIRECTORY: ITS (NOW) EMPTY"
                    elif [ -e $i ];then
                            echo "DELETING FILE $i"
                    else
                            echo UNKNOWN FILE $(basename $i)
                    fi
            done
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So at the moment I'm trying to delete files listed in the directory that
I am trying to write a shell script to delete all the sub directories
I'm trying to remove a directory using python but I do not want to
I'm trying to remove all files found in a directory. The accepted answer to
I am trying to remove a directory from my repository. I delete the folder
I'm trying to delete a directory that contains XML files from a remote computer.
I'm trying to remove part of the path in a string. I have the
Hello I was trying to remove objects from object array that I have and
I'm trying to delete a certain file within the directory that I'm running my
I'd like to delete all the files in a given directory on a remote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.