how to remove all subfolders except ones with foldername X using Terminal [closed]

天大地大妈咪最大 提交于 2019-12-12 03:53:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!