Discussion Locked

Shell script to clear file from homedirs

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello,
What's the best way to write a shell script to clear a file (procmail.log) from all users' home directories?
Can I use a shell script or do I need perl?
Thanks in advance.

7 replies

by ryttinch

Try something like the following if you want to just do a quick shell script:

#!/bin/sh

OUTPUTFILE=/tmp/procmail.log.output

for i in `awk -F: '{print $6}' /etc/passwd`
do
rm $i/procmail.log 2>> $OUTPUTFILE
echo $i/procmail.log removed >>$OUTPUTFILE
done

That should do what you're looking for (parsing the /etc/passwd file for the home directories, then deleting the procmail.log file from the home directories that it parses).

Hope this helps,

Chad Rytting

by ken.cunningham

Assuming all your users' home directories are under one particular mount point or similar, the easiest way might be simply to execute a find for the files in question from the directory above the home directories, and execute an rm on those discovered. This should do the trick:

cd
find . -name 'procmail.log' -exec rm {} \;

This, of course, assumes that the files should be deleted and not merely truncated! Hope this helps

by insatiable

You shouldn't need perl. I just posted this on another question, but it can be used in your case, also, modified to hit all user home directories.

This script is from a Unix Guru daily tip. The script will zero out a .log file. I keep it in my directory and just change the log file.

for object in procmail.log
do
> $object
print "$object has been zeroed!"
done

Good luck!
Melissa

This discussion is closed to new replies.