5

I have a directory that I is filled by another user, and I'm tasked with maintaining it. I want to delete all its content except a 2 files with a specific name. Is it possible with rm or should I do this:

cp aaa/a ./a && cp aaa/b ./b && rm -rf aaa/* && mv ./a aaa/a && mv ./b aaa/b

where aaa is the directory, a,b are the files I want to keep, and there are (at least, there may be) other files/directories in there.

Is there a better (and shorter) way?

1 Answers1

10

With bash extended globs, given

$ tree aaa
aaa
├── a
├── b
├── c
├── d
├── e
└── subdir

then

rm -rf aaa/!(a|b)

leaves

$ tree aaa
aaa
├── a
└── b

0 directories, 2 files
steeldriver
  • 142,475