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 ;).


# Pass directory path and pattern as arguments
cd $DIR
SUMMARY=`printf "User: $USER\nDIR: $DIR\nPattern: $PATTERN\nFiles Deleted:-"`
for i in `ls $PATTERN`
SUMMARY=`printf "$SUMMARY\n$i"`
rm -f $i
printf "$SUMMARY" | mailx -s "Files Clean Up"





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: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s