views:

139

answers:

2

Hi , I have many directories and need to delete them periodically with minimum time. Additionally for each directories delete status need to know i.e whether deleted successfully or not. I need to write on the ksh . Could you please help me out.

The sample code which I am using, in which I tried launching rm-rf in the background, is not working.

for var1 in 1...10
rm -rf <DIR> &
pid[var1]=$!
done

my_var=1
for my_var in 1...var1
wait $pid[my_var]
if [ $? -eq 1 ]
then
  echo falied
else
 echo passed
fi
done
A: 

You are out of luck. The bottleneck here is the filesystem, and you are very unlikely to find a filesystem that performs atomic operations (like directory deletion) in parallel. No amount of fiddling with shell code is going to make the OS or the filesystem do its job faster. There is only one disk, and if every deletion requires a write to disk, it is going to be slow.

Your best bet is to switch to a journaling filesystem that does deletions quickly. I have had good luck with XFS deleting large files (10-40GB) quickly, but I have not tried deleting directories. In any case, your path to improved performance lies in finding the right filesystem, not the right shell script.

Norman Ramsey
A: 

This is generally the form that your script should take, with corrections for the serious errors in syntax. However, as Norman noted, it's not going to do what you want. Also, wait isn't going to work in a loop like you seem to intend.

# this script still won't work
find . -type d | while read dir
# or: for dir in ${dirs[@]}
do
    rm -rf $dir &
    pid[++var1]=$!
done

for my_var in {1...$var1}
do
    wait ${pid[my_var]}
    if [ $? -eq 1 ]
    then
        echo failed
    else
        echo passed
    fi
done
Dennis Williamson

related questions