Operating System - HP-UX
1752862 Members
4440 Online
108791 Solutions
New Discussion

Shell scripting challenges.

 
kaushikbr
Frequent Advisor

Shell scripting challenges.

Hi All

I'm having a few challenges with a shell script.
In our environment, we have a few vxfs filesystems exported to users windows PCs.
The users create files and directories on these filesystems using the standard windows directory naming convention ( with spaces and and \ in directory names ). One of the softwares we run has a bug and creates direcotries recursively several levels deep.
These deep directories are causing all sorts of problem. I'm trying to write a script which can identify empty directories and delete them if they are empty. I do not have perl knowledge, hence I decided to write a shell script.

The directories can be in the form

'/var/tmp/TEST/directory 1'
'/var/tmp/TEST/directory 2\'
'/var/tmp/TEST/directory_3\'

and there can be several such directories and several levels deep.

I have to iterate through all these directories and delete the ones that are empty.


Thanks in advance for all your suggestions.

Kaushik
1 REPLY 1
Matti_Kurkela
Honored Contributor

Re: Shell scripting challenges.

Actually you don't need to identify if a directory is empty: a "rmdir" operation will be successful _only_ if a directory is empty.

To remove chains of otherwise empty directories, you'll need to walk the directory tree in so-called "depth-first" order:
If a directory has sub-directories, check them first; after that, if the directory has now become empty (i.e. the sub-directories were empty and were removed), remove the directory too.

If I understood your problem correctly, this one-liner should be all that's needed:

find /var/tmp/TEST -depth -type d -exec echo rmdir {} \+

NOTE: in the above form, the command will just echo out the command lines it would execute. Verify that the command does what you want, then remove the word "echo" to make the command actually delete stuff.

MK
MK