V
V
Viktor2016-03-15 20:59:40
linux
Viktor, 2016-03-15 20:59:40

How to remove all files and subdirectories from a directory, excluding a few specified subdirectories (and all the contents in them)?

There is a dir directory .
This directory has other subdirectories as well as files. I need to delete ALL contents of the dir directory EXCEPT the subdirectories poddir1 ( dir/poddir1 ) and poddir2 ( dir/somedir/poddir2 ) (do not delete the contents of these subdirectories).
Please tell me the correct command to solve this problem.
UPD: the subdirectories that need to be left have unique names.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Tretyakov, 2016-03-16
@babulja

ls | grep -v -E 'poddir1|poddir2' | xargs rm -rf
If you need to add another folder, then register it through the operator | , i.e:
And do not forget that you need to run the command while in the dir directory , otherwise sadness :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question