Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete all directories except one

Tags:

shell

posix

So using the shell, and having a directory:

./parent
./parent/one
./parent/two
./parent/three
./parent/four

i want to do something like rm -rf parent/* in one command, but keeping one directory, for example 'four', so the end result would be

./parent
./parent/four
like image 390
André Alçada Padez Avatar asked Dec 03 '15 02:12

André Alçada Padez


People also ask

How do I select all folders except one?

Select multiple files or folders that are not grouped together. Click the first file or folder, and then press and hold the Ctrl key. While holding Ctrl , click each of the other files or folders you want to select.


1 Answers

Easiest and practical way in my opinion is to move the exception to parent folder, delete everthing move it back

mv exceptionFolder ..
rm -rf *
mv ../exceptionFolder .
like image 83
Gediz GÜRSU Avatar answered Oct 08 '22 15:10

Gediz GÜRSU