My top-level directory is data
. data
includes several directories and these directories have sub-directories. I need to remove all files and directories inside data/
except several files in some directories.
For example, data includes the directories 100
and 101
. I just want to keep a.txt
and b.txt
files in 100/
and c.txt
and d.txt
files in 101/
while removing all other files and directories in 100
and 101
.
Example:
.
├── 100
│ ├── a.txt
│ ├── b.txt
│ ├── c.txt
│ └── d.txt
└── 101
├── a.txt
├── b.txt
├── c.txt
└── d.txt
I use rm -rf !(a.txt|b.txt)
command but I can't apply this command for each directory automatically.
Best Answer
As you already found out you can use the
extglob
feature which is enabled with:This allows to exclude matches so that you can do things like:
It's a good idea to test it with
echo
first. This example will match anything inside100/
which is nota.txt
orb.txt
and anything inside101/
which is notc.txt
ord.txt
. If the same rules for100/
apply to102/
as well you can do e.g.: