Home > Mobile >  delete all files except a pattern list file
delete all files except a pattern list file

Time:07-14

I need to delete all the files in the current directory except a list of patterns that are described in a whitelist file (delete_whitelist.txt) like this:

(.*)dir1(/)?
(.*)dir2(/)?
(.*)dir2/ser1(/)?(.*)
(.*)dir2/ser2(/)?(.*)
(.*)dir2/ser3(/)?(.*)
(.*)dir2/ser4(/)?(.*)
(.*)dir2/ser5(/)?(.*)

How can I perform this in one bash line?

CodePudding user response:

Any bash script can fit on one line:

find . -type f -print0 | grep -EzZvf delete_whitelist.txt | xargs -0 printf '%s\n'

Check the output and then, if it's OK:

find . -type f -print0 | grep -EzZvf delete_whitelist.txt | xargs -0 rm
  • Related