Selective remove awk-bash

Hi

I have a folder that contain up to 1000 subfolder. Genereally i need just 2 or 3 of them and i can know apriori the name of the subfolder i want to keep.
I'd like to erase all the unnecessary subfolders.

I've tried with:

ls -l | awk '$8 !~ /name_fold/{print "rm -r " $8}'| sh

and it works becouse i keep the name_fold1 ...but just for 1 folder, I'd like to do something like:

ls -l | awk '$8 !~ /name_fold1 || name_fold2 /{print "rm -r " $8}'| sh

in order to erase almost all and kepp the name_fold1 and name_fold2

Any idea?

thanks

D

Something like this?

ls -l | awk '$8 !~ /name_fold1/ && $8 !~ /name_fold2/{print "rm -r " $8}'| sh

Shorter :rolleyes:

ls -l | awk '$8 !~ /name_fold[12]/{print "rm -r " $8}'| sh

that's great!

thanks both :b:

Some shells support extended globbing:

ksh

rm -r -- !(name_fold[12])

bash

shopt -s extglob
rm -r -- !(name_fold[12])

zsh (setopt extendedglob if not enabled):

rm -r -- ^name_fold[12]