3

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

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.

| improve this answer | |
  • 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 Jun 28 '16 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. – Satō Katsura Jun 28 '16 at 17:20
  • Works perfectly !! Thanks for your help !! – Balaji Jun 28 '16 at 17:58
1

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.