macos 如何递归删除 UNIX 目录中的所有隐藏文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5061638/
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
How do you recursively delete all hidden files in a directory on UNIX?
提问by SharkCop
I have been searching for a while, but can't seem to get a succinct solution. I have a Mac with a folder that I want to clean of all hidden files/directories - anything hidden. It used to be a Eclipse workspace with a lot of .metadata/.svn stuff, and I'm fine with all of it being removed. How can I do this (either with a shell script, Applescript, etc). Thanks a lot in advance!
我一直在寻找一段时间,但似乎无法得到一个简洁的解决方案。我有一台带有文件夹的 Mac,我想清除所有隐藏的文件/目录 - 任何隐藏的内容。它曾经是一个包含大量 .metadata/.svn 内容的 Eclipse 工作区,我对所有这些内容都被删除感到满意。我该怎么做(使用 shell 脚本、Applescript 等)。非常感谢!
回答by jmq
find . -name ".*" -print
find . -name ".*" -print
I don't know the MAC OS, but that is how you find them all in most *nix environments.
我不知道 MAC 操作系统,但这就是您在大多数 *nix 环境中找到它们的方式。
find . -name ".*" -exec rm -rf {} \;
find . -name ".*" -exec rm -rf {} \;
to get rid of them... do the first find and make sure that list is what you want before you delete them all.
摆脱它们...... 在删除它们之前先进行查找并确保该列表是您想要的。
The first "."
means from your current directory. Also note the second ".*"
can be changed to ".svn*"
or any other more specific name; the syntax above just finds all hidden files, but you can be more selective. I use this all the time to remove all of the .svn directories in old code.
第一种"."
方法来自您当前的目录。另请注意,".*"
可以将第二个更改为".svn*"
或任何其他更具体的名称;上面的语法只是找到所有隐藏的文件,但你可以更有选择性。我一直使用它来删除旧代码中的所有 .svn 目录。
回答by Paused until further notice.
You need to be very careful and test any commands you use since you probably don't want to delete the current directory (.
), the parent directory (..
) or all files.
您需要非常小心并测试您使用的任何命令,因为您可能不想删除当前目录 ( .
)、父目录 ( ..
) 或所有文件。
This should include only files and directories that begin with a dot and exclude .
and ..
.
这应该只包括以点开头并排除.
和的文件和目录..
。
find . -mindepth 1 -name '.*' -delete
回答by mamuso
rm -rf `find . -type f -regex '.*/\.+.+'`
If you want to delete directories, change -type f
for type -d
.
如果要删除目录,请更改-type f
为type -d
.
If you want to delete files and directories remove type -f
如果要删除文件和目录,请删除 type -f
回答by kurumi
find /path -iname ".*" -type f -delete ;
Ruby(1.9+)
红宝石(1.9+)
ruby -rfileutils -e 'Dir["**/.*"].each{|x| FileUtils.rm(x) if File.file?(x)}'
回答by tahoar
I use this command to delete empty directories. It starts at the bottom and works its way to the top. So, it won't doesn't fail if you reference the current path.
我使用这个命令来删除空目录。它从底部开始,一直到顶部。因此,如果您引用当前路径,它不会失败。
find . -depth -type d -empty -exec rmdir {} \;
回答by conradkleinespel
I found this to work quite well (in Bash on Linux at least):
我发现这很有效(至少在 Linux 上的 Bash 中):
find . -wholename '*/.*' -type f | sed -n '/\/\.[^\/]\+$/p' | xargs rm
You can tweak the regular expression in the sed
call to your likings.
您可以sed
根据自己的喜好调整调用中的正则表达式。
Be careful though: in my case, I have a lot of hidden files named .gitignore
or .gitkeep
that must be preserved. Be sure to check the list to see if anything is in there that you want to keep.
不过要小心:就我而言,我有很多已命名.gitignore
或.gitkeep
必须保留的隐藏文件。请务必检查列表以查看其中是否有您想要保留的内容。
I've found this variant to be quite useful, it removes files like ._ANYTHING
(often trashed or tmp files):
我发现这个变体非常有用,它可以删除文件._ANYTHING
(通常是垃圾文件或 tmp 文件):
find . -wholename '*/.*' -type f | sed -n '/\/\._[^\/]\+$/p' | xargs rm