Tuesday, September 30, 2014

Delete all files except files that match expression

If you need to delete all files that do not match a certain expression, e.g. all the files that are not ending with gz, use find command

First test if the code is picking up the files you really want to delete, (since removal is permanent it is good to be sure what you're removing):

find . \! -name '*gz' 

Then add -delete

find . \! -name '*gz' -delete

Similarly, to delete files that do match expression, remove \!

Check the selection:

find . -name '*gz'


find . -name '*gz' -delete

No comments:

Post a Comment