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

Nathan Friend nathan.friend at gmail.com
Fri Jul 16 13:36:28 UTC 2010


That's a good idea. thanks.

while read dir
do
if [ -d $dir ]
then
rm -R $dir
echo "Directory $dir found and deleted."
else
echo "Directory $dir not found."
fi
done < dir_list


dir_list file contains
/Users/user1
/Users/user2
/Users/user3


On Fri, Jul 16, 2010 at 1:18 PM, Laurence Southon
<laurence at southon.uk.net>wrote:

> 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
>
> _______________________________________________
> Kent mailing list
> Kent at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/kent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/kent/attachments/20100716/1c71f572/attachment.htm>


More information about the Kent mailing list