Operating System - HP-UX
1753691 Members
5582 Online
108799 Solutions
New Discussion

How to delete empty subdirectories

 
CKT
Advisor

How to delete empty subdirectories

Does any one know a quick way to find all empty subdirectories under a given directory (/mydir) and delete them? I tried the following command and it only work for some:

find /mydir -type d -size 2 -exec rmdir -f {} ;

-size 1 does not work for all either.
You can't turn back the clock, but you can always rewind it again.
1 REPLY 1
Bill Hassell
Honored Contributor

Re: How to delete empty subdirectories

A rather brute force way is to:

find -xdev -type d -exec rmdir {} ; 2> /dev/null

What this does is to remove all directories that are empty and bypass any directories that contain files or other directories. rmdir will not remove a directory which has files or other directories. The 2> /dev/null eliminates the error message from rmdir when a directory is not empty. -xdev prevents going below a mountpoint for another filesystem.


Bill Hassell, sysadmin