[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Checking if directory is empty in postrm



Richard Braakman writes:
 > Marcus Brinkmann wrote:
 > > can someone give me a hint how I can efficient test if a certain directory
 > > is empty in the postrm (shell script)?
 > 
 > If you only want to know if it's empty because you want to remove it,
 > then there's a very simple solution:
 > 
 > rmdir directory 2>/dev/null || true
 > 
 > The rmdir will fail if it's not empty :)
 > (You can use its return code if you need to check if it succeeded)

But this will leave directories with empty directories ... another
alternative, but more expensive:

for d in `find DIR -depth -type d -empty 2> /dev/null`; do
  while rmdir $d 2> /dev/null; do d=`dirname $d`; done
done

Of course you can rid off the find, if you know, what your empty
directories are.


Reply to: