Delete empty directories recursively - HP-UX


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Delete empty directories recursively - HP-UX
# 8  
Old 06-03-2011
Let me try.
# 9  
Old 06-03-2011
Try this:
Code:
#! /bin/ksh

function remove_directory
{
        typeset olddir ok
        olddir=$(pwd)
        cd $1
        ok=1
        for name in *  .* ; do
                if [[ $name = . ]] ; then
                        continue
                elif [[ $name = .. ]] ; then
                        continue
                elif [[ $name = '*' ]] ; then
                        continue
                elif [[ -d $name ]] ; then
                        remove_directory $name  || ok=0
                else
                        ok=0
                fi
        done
        cd $olddir
        if ((ok)) ; then
                rmdir $1
                return 0
        else
                return 1
        fi
}

remove_directory $1
exit 0

# 10  
Old 06-03-2011
Hello, Perderabo:

A minor nit which in practice is very unlikely to occur, but I will point it out because these little corner cases can be educational for novices.

Your code will misbehave if it encounters a directory named '*'. Instead of recursing, it'll skip over it. If that skipped directory and any directories it contains are all otherwise empty, that part of the tree will not be removed.

My suggested fix for this approach would be to AND a negated test for existence to the '*' equality test.

Regards,
Alister

Last edited by alister; 06-03-2011 at 11:46 AM..
# 11  
Old 06-03-2011
That's an intentional feature. This allow the user to create a directory which will never be deleted by the script. SmilieSmilieSmilie
# 12  
Old 06-03-2011
Hehehe. As always, you're one step ahead. Smilie
# 13  
Old 06-07-2011
This one
find . -type d -depth -exec rmdir {} + 2>/dev/null
works perfect.

Thanks a lot.

---------- Post updated at 02:30 PM ---------- Previous update was at 02:26 PM ----------

My thanks to everyone who spent their time. I could not test all the approaches and I am sure there might be other options. The one find . -type d -depth -exec rmdir {} + 2>/dev/null is neat and clean. There are oppornunities of lot of learning from this forum...
Asutosh
# 14  
Old 06-08-2011
Quote:
find . -type d -depth -exec rmdir {} + 2>/dev/null
Scary piece of code. Tries to remove every directory in the tree one-by-one and ignores all errors.

Hmm. Removes all empty directories on the first pass. Needs multiple passes to remove directories which have become empty.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generic script to recursively cd into directories and git pull

Hi all, I'm trying to write a script to recursively cd into my Git projects and pull them, and will later expand it to build my projects as well. I'm having a bit of trouble with my current script, as I want to supply a command line argument to tell it which branch to check out. I can hard... (2 Replies)
Discussion started by: Cows
2 Replies

2. Shell Programming and Scripting

Recursively Searcing file in the directories

i have directory dgf in the dgf( some other Sub-dir are there) 00 01 02 03 04 in all the Sub directory there is a SG.csv .. i want the scripts should run one by one Sub-dir and print the result for that particular Sub-dir ..then go to next Sub-Dir and print the result....... please... (6 Replies)
Discussion started by: Aditya.Gurgaon
6 Replies

3. Shell Programming and Scripting

Shell script to copy particular file from directories recursively

I have directory path in which there are several sub directories. In all these sub dir there will be one env.cnf file. I want to copy this env.cnf file from each sub dir's and place them in destination path by creating same filename as sub dir_env.cnf. After copying env.cnf files from source... (4 Replies)
Discussion started by: Optimus81
4 Replies

4. Homework & Coursework Questions

Find and delete empty files and directories

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Need to make a script, to remove all empty files and folders from current category. It also should show the name... (2 Replies)
Discussion started by: Itixop
2 Replies

5. Shell Programming and Scripting

Recursively rename directories

I have this directory tree under /apps/myapp/data: imageshack.us/photo/my-images/703/foldersc.png How to recursively rename ONLY directories with 5 digits (00000, 00100, 00200,..., 00007, 00107,...)? I want to add to their name two more zeros: Before: 00107 After: 0000107 Thanks in... (2 Replies)
Discussion started by: Susan_45
2 Replies

6. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

7. UNIX for Advanced & Expert Users

Recursively delete only specified directories with given pattern

Hi All, We have a requirement to recursively delete the directories and its subdirectories older than 60 days based on timestamp (folder creation timestamp)under certain directory. However it has some specific requirements. The directories will continue to be there upto any depth. the... (0 Replies)
Discussion started by: rcvasu
0 Replies

8. UNIX for Dummies Questions & Answers

How to display directories recursively?

Cannot find how to list the directory structure of a volume recursively. Do not want the files reported. Say I have 100 directories and 10,000 files, I do not want 10,000 lines of output. (If this is relevant, I am using the terminal on my OSX Mac). I hope this is easy - there should be an easy... (5 Replies)
Discussion started by: jwriter
5 Replies

9. UNIX for Dummies Questions & Answers

Recursively deleting directories

Say I have a directory call test, and several directories nested in it, and several directories nested in them. And I want to remove all directories within "test" and its subdirectories that have the name "cvs", how can I do this? I tried rm -r cvs, but that only removed the top level direcotry... (4 Replies)
Discussion started by: mikeshank
4 Replies
Login or Register to Ask a Question