[Klug-general] Bash script to delete folders if they exists

Laurence Southon laurence at southon.uk.net
Fri Jul 16 12:18:11 UTC 2010


On 16/07/10 09:42, Nathan Friend wrote:
> Thanks Karl that was easy.
> 
> if [ -d /home/nathan/test2/123 ]
> then
> rm -r 123
> echo "123 deleted"
> else
> echo "123 not found"
> fi
> 
> if [ -d /home/nathan/test2/456 ]
> then
> rm -r 456
> echo "456 deleted"
> else
> echo "456 not found"
> fi
>
If that list of directories is long or might change, you could put it in
a file /home/nathan/dir_list, one per line, and then make the script
loop through the list of entries:

while read dir

do

if [ -d $dir ]
then
.

.
fi
done < /home/nathan/dir_list

Then you just need to change dir_list and not the script.

LS
-- 
Laurence Southon
Tiger Computing, Bexley
www.tiger-computing.co.uk



More information about the Kent mailing list