If we want to delete all files and directories we use, rm -rf *
.
But what if i want all files and directories be deleted at a shot, except one particular fi
find can be a very good friend:
$ ls
a/ b/ c/
$ find * -maxdepth 0 -name 'b' -prune -o -exec rm -rf '{}' ';'
$ ls
b/
$
Explanation:
find * -maxdepth 0
: select everything selected by *
without descending into any directories
-name 'b' -prune
: do not bother (-prune
) with anything that matches the condition -name 'b'
-o -exec rm -rf '{}' ';'
: call rm -rf
for everything else
By the way, another, possibly simpler, way would be to move or rename your favourite directory so that it is not in the way:
$ ls
a/ b/ c/
$ mv b .b
$ ls
a/ c/
$ rm -rf *
$ mv .b b
$ ls
b/