I was writing some scripts and wrote something like
ARTIFACTS="/SOME/PATH"
[ -d $ARTIFCATS ] && rm -rf $ARTIFACTS/*
What happened is that out of stupidity I executed the second line without executing the first. It turned out that [ -d "" ] returns true and the expression became
rm -rf /*
Luckily it was only a test machine and I wasn't a sudo, but though I lost some data
My question is, why [ -d "" ] return true?? the documentation clearly states it checks whether a path exists and is a folder
I solved the problem by using
[ -e $ARTIFACTS ]
which seems to work
Cheers
rm -rf $ARTIFACTS
without the/*
. This would also delete the$ARTIFACTS
directory, which is fine, because if i want to be sure that it exists before putting something in it, i will executemkdir -p $ARTIFACTS
anyway. It will also delete hidden files inside$ARTIFACTS
, which is also fine, because i wouldn't writerm -rf $ARTIFACTS/*
if$ARTIFACTS
contained anything i wanted to save.