[K12OSN] shell script to remove contents of several files

Petre Scheie petre at maltzen.net
Fri May 19 13:30:59 UTC 2006

If each user's mail is stored in a single file that is the same name as the user 
ID--that is, Bob's mail file is called bob--you could do something like this:

for x in $(ls /var/mail/allusers/)

This will reduce each file in /var/mail/allusers/ to zero bytes (notice the > at the 
beginning of the third line) without removing the file itself.  If each user's mail is 
made up of multiple files stored within a single respective directory--that is, Bob's 
mail is multiple files stored in /var/mail/allusers/bob--you could do this:

for x in $(ls /var/mail/allusers/)
rm -rf /var/mail/allusers/${x}/*

I avoid using cd commands with rm because if the cd command fails on one directory--say 
the directory has spaces or other unusual characteristics--it won't prevent the rm 
command from running and it could mistakenly wipe out your whole parent directory; 
better to use the full path in the rm command.


Barry Cisna wrote:
> Hello All,
> Due to the fact ,that I am not smart enough, would someone be able to
> give an example of a shell script that would remove the contents of each
> users, email folder,( but of course leave the user's folder on the
> system). I'm wanting to "clean out" everyone's email at the end of the
> school year.
> In other words something like delete contents
> of /var/mail/allusers<folder contents>.
> I'm not sure of correct syntax of "rm" or "shred"
> Thanks again,
> Barry Cisna
> _______________________________________________
> K12OSN mailing list
> K12OSN at redhat.com
> https://www.redhat.com/mailman/listinfo/k12osn
> For more info see <http://www.k12os.org>

More information about the K12OSN mailing list