Ubuntu – How to delete all the files/folders from the folder except few folders

deleterm

Assume that I have a directory structure as follows:

application
│
├── config
│   ├── c1.conf
│   └── c2.conf
│
├── logs
│   ├── l1.txt
│   └── l2.txt
│
└── src
    ├── static
    ├── s1.py
    └── s2.py

Now I want to delete all the folders except config and logs (in this case, only src). my PWD is currently a parent of appliaction/ dir (i.e. I can see application in my PWD).

What command should I use?
(I tried with rm -rf with some options but mistakenly deleted other files, so I would like to know the correct answer before trying anything else!)

Best Answer

Try find ./application/ -type d -not -name config -not -name logs.

If that returns the proper directories, run

find ./application/ -type d -not -name config -not -name logs -exec rm -R {} \;

The exec rm -R {} \; at the end removes the directory even if it is not empty.