[K12OSN] scripting trash removal

Les Mikesell les at futuresource.com
Fri Jan 27 21:32:43 UTC 2006


On Fri, 2006-01-27 at 15:35, Calvin Dodge wrote:
> 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.

The succinct way would be
 cd /home/${x}/.Trash && rm -rf *
(&& means continue if the previous command succeeded)
or just
rm -rf /home/${x}/.Trash/*
as long as the expansion fits in the command line size limit.

-- 
  Les Mikesell
   les at gmail.com





More information about the K12OSN mailing list