0

I have a list of folders that all have a year in parentheses, except for a few:

./Folder 1 (2001)
./Folder 2 (2008)
./Folder 3
./Folder 4 (1994)
./Folder 5 (2005)
./Folder 6

I would like to delete the folders (and contents therein) that don't have a parentheses in their name via the command line. In the above example, Folder 3 and Folder 6.

I know that if I wanted to delete all the folders that do have a parentheses in their title I could do:

rm -rf *(*

But I don't know how to do it the other way around.

1

With bash's extended globbing (which should be enabled by default, in Ubuntu)

$ shopt extglob 
extglob         on

it should be possible to use the glob pattern !(*\(*\)*)

e.g. given

$ tree
.
├── Folder 1 (2001)
├── Folder 2 (2008)
├── Folder 3
├── Folder 4 (1994)
├── Folder 5 (2005)
└── Folder 6

6 directories, 0 files

then

$ rm -ri !(*\(*\)*)
rm: remove directory 'Folder 3'? y
rm: remove directory 'Folder 6'? y

leaving

$ tree
.
├── Folder 1 (2001)
├── Folder 2 (2008)
├── Folder 4 (1994)
└── Folder 5 (2005)

4 directories, 0 files
0

In bash, use GLOBIGNORE:

$ echo *
Folder  1 (2001) Folder  2 (2008) Folder  3 Folder  4 (1994) Folder  5 (2005) Folder  6
$ GLOBIGNORE='*\(*'
$ echo *
Folder  3 Folder  6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.