[K12OSN] scripting trash removal

Calvin Dodge caldodge at fpcc.net
Fri Jan 27 21:35:54 UTC 2006


On Fri, Jan 27, 2006 at 01:22:20PM -0500, Mark Gumprecht wrote:
> Dave was nice to send me this for clearing cache
> 
> !#/bin/bash
> for x in `ls /home`; do
> echo "Doing $x ..."
> cd /home/${x}/.mozilla/firefox/*.default/Cache
> rm -Rf *
> done
> 
> Ok that works just fine. Soooo, If that works for cache, with a little 
> mod like this
> 
> !#/bin/bash
> for x in `ls /home`; do
> echo "Doing $x ..."
> cd /home/${x}/.Trash
> rm -Rf *
> done
> 
> It should clear out the .Trash directory only... right?
> I Ran the script and it deleted out the trash, but it also deleted all 
> non hidden files in the root directory from which I ran the script.. 
> including itself.
> I'm sure it is obvious to someone who knows, but I don't see why it is 
> killing all the files.

Well, if a user doesn't have a .Trash directory, then the "cd" will fail,
and the script will merrily delete whatever is in the current directory.

Also, I believe a malicious user could do "rm -Rf ~/.Trash;ln -s /root .Trash",
and fool your script into deleting the contents of /root.

I'm sure some Linux guru can come up with a more elegant way to deal with this,
but I'd do something like:

if [ -d /home/${x}/.Trash ]
then
  echo "Doing $x ..."
  cd /home/${x}/.Trash
  rm -Rf *
fi

The inner part will execute only if .Trash is a directory.

-- 
Calvin Dodge
Certified Linux Bigot (tm)
http://www.caldodge.fpcc.net




More information about the K12OSN mailing list