ronen
ronen

Reputation: 1490

delete all files except a pattern list file

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?

Upvotes: 0

Views: 234

Answers (1)

Renaud Pacalet
Renaud Pacalet

Reputation: 28965

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

Upvotes: 1

Related Questions