bash 递归删除服务器上的 CVS 目录的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1330136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Script to recursively delete CVS directory on server
提问by meder omuraliev
So far I've come up with this:
到目前为止,我想出了这个:
find . -name 'CVS' -type d -exec rm -rf {} \;
It's worked locally thus far, can anyone see any potential issues? I want this to basically recursively delete 'CVS' directories accidently uploaded on to a server.
到目前为止,它在本地工作,有人能看到任何潜在的问题吗?我希望这基本上可以递归删除意外上传到服务器的“CVS”目录。
Also, how can I make it a script in which I can specify a directory to clean up?
另外,如何使它成为可以指定要清理的目录的脚本?
采纳答案by derobert
Well, the obvious caveat: It'll delete directories named CVS, regardless of if they're CVS directories or not.
嗯,明显的警告:它会删除名为 CVS 的目录,无论它们是否是 CVS 目录。
You can turn it into a script fairly easily:
你可以很容易地把它变成一个脚本:
#!/bin/sh
if [ -z "" ]; then
echo "Usage: ?
case "" in
/srv/www* | /home)
true
;;
*)
echo "Sorry, can only clean from /srv/www and /home"
exit 1
;;
esac
?
path"
exit 1
fi
find "" -name 'CVS' -type d -print0 | xargs -0 rm -Rf
# or find … -exec like you have, if you can't use -print0/xargs -0
# print0/xargs will be slightly faster.
# or find … -exec rm -Rf '{}' + if you have reasonably modern find
edit
编辑
If you want to make it safer/more fool-proof, you could do something like this after the first if/fi block (there are several ways to write this):
如果你想让它更安全/更万无一失,你可以在第一个 if/fi 块之后做这样的事情(有几种写法):
##代码##You can make it as fancy as you want (for example, instead of aborting, it could prompt if you really meant to do that). Or you could make it resolve relative paths, so you wouldn't have to always specify a full path (but then again, maybe you want that, to be safer).
您可以随心所欲地制作它(例如,它不会中止,而是会提示您是否真的打算这样做)。或者你可以让它解析相对路径,这样你就不必总是指定一个完整的路径(但话说回来,也许你想要这样,更安全)。
回答by choudeshell
A simple way to do would be:
一个简单的方法是:
find . -iname CVS -type d | xargs rm -rf
找 。-iname CVS -type d | xargs rm -rf

