Suppose under the current directory, there are multiple sub-directories, and one is called A.
How to delete all sub-directories except A with Bash?
|
|
|
Bash has extended globbing (first test, then remove the echo):
|
|||||
|
|
|||||
|
|
Something like
should do. edit It should really be
to prevent |
||||
|
What about:
This avoids some of the "scariness" of a typo in the other commands. |
|||||||
|
|
i usually do this by working up an ls command that gets it right first. i'm not at a unix machine, but something like: ls -lda "[^A]" Once you get it right, pipe it to a command ls -lda "[^A]" | xargs rm -rf Feel free to edit above if I've got my regular expression wrong... |
||||
|
|
|
If you want to be more flexible but manual you can do:
That way you can do general munging. |
||||
|
|
|
Here's one way. Be careful with this sort of thing, though, it's so powerful that it can only be used for good or evil... find * -type d | grep -v "^A" | xargs rm -rf |
||||
|
|
|
Don't use find as some people have shown with -exec and rm without passing -print0 to find and -0 to xargs. It will get confused on file names with spaces or newlines:
Instead use find -print0 with xargs -0 , '-exec command {} +', or -delete if your find supports it. |
||||
|
|
|
See also here: |
||||
|
|