In BASH you can use the trailing slash (I think it should work in any POSIX shell):
rm -R -- */
Note the --
which separates options from arguments and allows one to remove entries starting with a hyphen - otherwise after expansion by the shell the entry name would be interpreted as an option by rm
(the same holds for many other command line utilities).
Add the -f
option if you don't want to be prompted for confirmation when deleting non-writeable files.
Note that by default, hidden directories (those whose name starts with .
) will be left alone.
An important caveat: the expansion of */
will also include symlinks that eventually resolve to files of type directory. And depending on the rm
implementation, rm -R -- thelink/
will either just delete the symlink, or (in most of them) delete the content of the linked directory recursively but not that directory itself nor the symlink.
If using zsh
, a better approach would be to use a glob qualifier to select files of type directory only:
rm -R -- *(/) # or *(D/) to include hidden ones
or:
rm -R -- *(-/)
to include symlinks to directories (but because, this time, the expansion doesn't have trailing /
s, it's the symlink only that is removed with all rm
implementations).
With bash
, AT&T ksh
, yash
or zsh
you can do:
set -- */
rm -R -- "${@%/}"
to strip the trailing /
.
You don't need to recursively enumerate directories to delete them with rm -rf
; you can simply list the top-level directories you want to delete. To determine whether a directory entry is a directory rather than a file, you can use find
's -type d
test; using .
isn't a good indicator.
The following should work for you:
find * -maxdepth 0 ! -name encoded -type d -print0 | xargs -0 rm -rf
This starts from all the entries in the current directory (to avoid listing .
), drops anything whose name is encoded
, and keeps only directories; it outputs the result using a null as delimiter, and feeds the result into xargs
to give to rm -rf
. Before running this variant, try
find * -maxdepth 0 ! -name encoded -type d
to see if the output matches what you expect.
-prune
doesn't ignore its match, it avoids descending into it; so in your example, encoded
is still listed, and then filtered by the -name
test.
Best Answer
You can just throw away the error messages:
That'll throw away all errors. If you want to see other potential errors then we can do something more complicated:
In this way other errors will still be logged.