简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM