Shell script delete files by pattern

Respected Readers,

Sharing a shell script that delete all files in a particular dir that match a pattern. The same can also be achieved by a combination of find and rm commands. This script can be called from a master script that is responsible for cleaning up temp or intermediate files generated by application. It emails list of all files deleted and user who invoked the script ;).


#!/usr/bin/ksh

# Pass directory path and pattern as arguments
DIR=$1
PATTERN=$2
cd $DIR
USER=`whoami`
SUMMARY=`printf "User: $USER\nDIR: $DIR\nPattern: $PATTERN\nFiles Deleted:-"`
for i in `ls $PATTERN`
do
SUMMARY=`printf "$SUMMARY\n$i"`
rm -f $i
done
printf "$SUMMARY" | mailx -s "Files Clean Up" someonecool@somewherecool.com

 

Thanks,

Badal

Advertisements

About Badal Chowdhary

I am a Software Engineer by profession. I have done SCJP and SCWCD certifications. Like working on cutting edge technologies and frameworks. Driven by challenges and fascinated by technology. I love playing and watching sports: Cricket, Ping Pong, Tennis, Badminton, Racket Ball and Gym.
This entry was posted in Unix and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s