问题
What is the best way to remove all subfolders except ones with folder name X and Y using Terminal.
回答1:
find . -mindepth 1 -d -type d ! -name X -a ! -name Y -exec rm -r {} \+
mkdir d; touch d/f; find . -name d -delete doesn't seem to work.
-mindepth 1 and -d are optional at least on OS X. Without -mindepth -1 there would be a warning like rm: "." and ".." may not be removed. Without -d rm would try to delete subfolders after deleting their parent folders.
{} doesn't have to be escaped.
If all directories are under the current directory:
shopt -s extglob
rm -r !(X|Y)/
来源:https://stackoverflow.com/questions/15785981/how-to-remove-all-subfolders-except-ones-with-foldername-x-using-terminal