Operating System - HP-UX
1752794 Members
5568 Online
108789 Solutions
New Discussion юеВ

Re: Remove empty directory

 
Mah_1
Occasional Contributor

Remove empty directory

Hi ,

Can anyone tell me how to remove empty directories which are created before 30 days .
ie i have to remove empty directores from /usr1 filesystem.

Thanks
8 REPLIES 8
Kranti Mahmud
Honored Contributor

Re: Remove empty directory

Hi mah,


Please execute the following command:

enter the diectory and execute: ls -al

If the directory is empty then execute:

rm -rf /user1

Rgds-kranti
Dont look BACK as U will miss something INFRONT!
Mah_1
Occasional Contributor

Re: Remove empty directory

There are lakhs of directories.
i want a command which will find out and delete those dirs.
Mark McDonald_2
Trusted Contributor

Re: Remove empty directory

I dont think I can do it with 1 command, you will need a script.

for DIR in $(find /usr1 -type d - mtime +30)
do
cd $DIR
EMPTY=`ls`
if [[ $EMPTY = "" ]]
then

rm $DIR
fi
done


You will need to tweak this before running on a live system. I am not in front of a unix box to get the commands exactly.
V. Nyga
Honored Contributor

Re: Remove empty directory

Hi,

Mark gave you the find command.
Do delete empty dirs check the command 'rmdir' - this command deletes empty directories (see 'man rmdir').

make a test before ...

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
Matthias Zander
Advisor

Re: Remove empty directory

all in one:

find /usr1 -type d -mtime +30 -depth -exec rmdir {} \; 2>/dev/null
Mah_1
Occasional Contributor

Re: Remove empty directory

thanks
Dennis Handly
Acclaimed Contributor

Re: Remove empty directory

>Matthias: find /usr1 -type d -mtime +30 -depth -exec rmdir {} \; 2>/dev/null

You might want to just redirect rmdir errors and not find errors. And use + for performance:
find /usr1 -type d -mtime +30 -depth -exec rmdir {} 2> /dev/null +
Dennis Handly
Acclaimed Contributor

Re: Remove empty directory

>ME: You might want to just redirect rmdir errors and not find errors.

Oops, the only way to get this to work is to create a script to do the rmdir and then redirect stderr:
find /usr1 -type d -mtime +30 -depth -exec rmdir_script {} +

rmdir_script:
#!/usr/bin/ksh
rmdir "$@" 2> /dev/null