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"
#!/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
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
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
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.
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..
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
Following code is working fine in bash but it is not working fine in sh
strange !
#!/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
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