Printing empty subdirs before delete

I am using following code to delete all the empty sub dirs from the inputted dir $1.
Before deleting empty dirs, I want to print those dirs which are going to be deleted.
Can this be done with little modification in following code

#!/bin/sh
if [ $1 -eq '' ]; then
echo "Searching '$1' dir for empty subdirs \n"
fi
find $1 -type d | xargs rmdir -ps
echo "All empty subdirs in '$1' are deleted successfully"
$ find $1 -type d -print -exec rmdir -ps {} \;

Thanks jayan_jay,
This works fine but one problem.
I have dir structure ./a/b/c/d/e/f/g/h ...it displays all of them even if they are not empty...

check whether your find supports -empty

No it doesnt (:-
I have modified script as below

#!/bin/sh
if [ $1 -eq '' ]; then
  echo "Searching '$1' dir for empty subdirs \n"
fi
echo "Following  empty dirs are going to be deleted \n"
ls -Rl * | awk '/total 0/{print last}{last=$0}'
find $1 -type d | xargs rmdir -ps
echo "All above empty subdirs in '$1' have been deleted successfully"

Now I need one more help. I need to display " No empty dir exists " is there are no empty dirs.
How to incorporate it in this script.
Thanks,
Ajay

just tried to tweak jayan code , give it try :

find $1 -type d -size 0 ac -print -exec rmdir -ps {} \; 

:p:)

This is not working . I am getting error

find: bad option ac
find: path-list predicate-list

---------- Post updated 01-12-12 at 12:24 AM ---------- Previous update was 01-11-12 at 04:21 AM ----------

Is there anyway to ls for $1 dir ...currently below ls is feting o/p from current dir.
I want to execute it for $1 dir

 ls -Rl * | awk '/total 0/{print last}{last=$0}' 

---------- Post updated at 05:07 AM ---------- Previous update was at 12:24 AM ----------

I actually require script as follows.

STEP 1 count number of empty dirs
STEP 2 if number of empty dirs is 0 echo "There are no empty dirs in this dir"
exit
else
print number of dirs
display all empty dir name
STEP 3 delete all empty dirs

Can any expert here help

Thanks,
A

 
count=$(ls -lR | grep -c "total 0")
echo $count
[ "$count" -eq "0" ] && echo "There are no empty dirs in this dir" || echo $count
#To Print the Empty directory
ls -lR | nawk '/^\./{f=$1}/^total 0/{print f}'

Thanks itkamaraj
This is working fine at command prompt but when i write it inside script I am getting
syntax error at line 1: `count=$' unexpected error

try with

count=`ls -lR | grep -c "total 0"`

i) This is storing ls -lR | grep -c "total 0" in $count ...and not the number of empty dirs...
ii)
ls -lR | nawk '/^\./{f=$1}/^total 0/{print f}'
displays those dirs also which has empty files ..is there anyway to exclude those.

what is mean by ?

This is storing ls -lR | grep -c "total 0" in $count ...and not the number of empty dirs...

---------- Post updated at 09:59 AM ---------- Previous update was at 09:50 AM ----------

Use back ticks, see the posting carefully :eek:

 
count=`ls -lR | grep -c "total 0"`
 

Sorry for confusion it is working fine...only thing it counts those dirs also which has empty files ..is there anyway to exclude those dir from the total count..

try this..

 
find . -type d | while read d; do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d; done | wc -l

Thanks itkamaraj for your help. It is working fine from command prompt but when I write it in the script I am getting
rmchk.sh: syntax error at line 1: `(' unexpected
Script

 
count=`find . -type d | while read d; do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d; done | wc -l`
echo $count

spoon feeding :frowning:

read my previous posts and do the change for script

Following code is working fine in bash but it is not working fine in sh
strange ! :confused:

#!/bin/bash

#Count number of subdirs
 
count=`find . -type d | while read d
do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d
done | wc -l`
echo "Number of empty subdir = $count "

#if count of empty subdirs is 0 exit the script 
if [ $count -eq 0 ]
then
echo "Exiting the script as there are no subdirs to delete "
exit 1
fi

for sh i am getting rmchk.sh: syntax error at line 1: `(' unexpected :mad:
Now I want to display and then delete all empty dirs ...efficent way to delete is
find . -type d | xargs rmdir -ps but not sure how I can print empty dir names before deletion
trying following
find . -type d | while read d; do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d; done ; -print
Thanks,
Ajay

---------- Post updated at 06:09 AM ---------- Previous update was at 02:24 AM ----------

Final Script : May be useful for all

#Validating dir parameter
if [ $# -eq 0 ]
then
echo "No parameters detected"
exit 1
fi

#Change dir to the dir to the parameter dir
cd $1

#Cleaning the log file
echo "" >> log.txt
echo "Checking $1 for empty subdirs"

#Count number of subdirs
count=`find . -type d | while read d
do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d
done | wc -l`
echo "Number of empty subdir in $1 = $count " > log.txt

#If count of empty subdirs is 0 exit the script
if [ $count -eq 0 ]
then
echo "Exiting the script as there are no subdirs to delete " >> log.txt
echo "`cat log.txt`"
mailx -s "Empty subdirs deleted at $1 on `date`" somone@somesite.com < log.txt
exit 1
fi

#Displaying all empty subdirs
find . -type d | while read d; do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d; done >> log.txt

#Deleting all the empty subdirs
find . -type d | xargs rmdir -ps
echo "All the empty subdirs in $1 are deleted successfully" >> log.txt
echo "`cat log.txt`"

#Sending email to team
mailx -s "Empty subdirs deleted at $1 on `date`" someone@somesite.com < log.txt