Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a script to search a directory and delete files

Status
Not open for further replies.

tkerr

Vendor
Jun 24, 2004
1
US
Hey all,
I need to create a script that searchs through the files of a directory for a string that would actually be inside of the file (it is actually an email address (ie test@test.com)), if the string is found, I need it to delete the file.

I have a mail filter queue on my firewall and it keeps getting cluttered with messages from the postmaster and the daemon. I need to delete any files in the queue that have come from the either of those mailboxes. I cannot delete all the contents of the directory because I would be getting rid of email that is supposed to be there.

I have never created a script before and am looking for some help, any you could offer would be appreciated.
Tim
 
man find
man fgrep (the -l option)
man xargs
man rm

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I'd suggest, especially since you're new to scripting, that you move the files to a holding area, rather than delete them. That way any misfire in your script will have less dire repercussions.

Something like this:
Code:
for file in `ls` 
do
grep test@test.com $file > /dev/null 2>&1 && mv $file other/dir
done

This searches each file for test@test.com, throwing away the output, and if it finds it, moves the file.



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top