I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory.

I know the following command works for one exception file but i need to check a list of exception files

find . ! -name 'file.txt' -type f -exec rm -f {} +

2 Answers 2


Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:

find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d '\n' rm -f

Replace rm -f with ls -1 for a dry run first. Put paths exactly as they are printed by find in exceptions.txt.

  • Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
    – Balaji
    Commented Jun 28, 2016 at 17:17
  • find prints a list of files, one per line. fgrep filters out the files in exceptions.txt. xargs ... rm -f removes the files. Commented Jun 28, 2016 at 17:20
  • Works perfectly !! Thanks for your help !!
    – Balaji
    Commented Jun 28, 2016 at 17:58

I don't think find has an option like this, you could build a command using printf and your exclude list:

find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f {} +

file.txt will have list of files to exclude in find command.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .