Remove the file except with particular extension

Hi all

i am new for the shell scripting can any one help me with my requirments . i want to delete file older than 21 days everything works fine but in that dir i got the files with should not be deleted with particular extension like (.info):confused:here is the script i wrote .can anyone help me how to twick the below script

for files in $DIR
do
echo "Looking for Files in $DIR"
find $DIR/* -mtime +21 -exec rm {} \;
echo "Deleted found files"
done

Use logical not ! to exclude such files:

! -name "*.info"
1 Like

Use tags for code, please.

find is already recursive -- looks inside folders -- so you don't need to put it in a loop.

It will also take a -name option to search for specific filename patterns. You can use '!' to make it exclude them instead.

find $DIR -mtime +21 '!' -name '*.info' -exec echo rm '{}' ';'

Right now it will just print 'rm filename' instead of actually running 'rm filename', just to test. Remove the echo once you've tested it and are sure it does what you want.

1 Like

Thanks every one for the suggestions will try the above and let you know the result:)

find /path/to/logs/ -mtime +10 '!' -name '*.info' -exec echo '{}' ';'

this works

thank you!!!!!

---------- Post updated 04-05-13 at 12:09 PM ---------- Previous update was 04-04-13 at 01:23 PM ----------

Hi how do i use the above command to exclude multiple file

1 Like
find /path/to/logs/ -mtime +10 ! \( -name '*.info' -o -name '*.extn' -o .... \) -exec echo '{}' ';'
1 Like