Linux 递归删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2016844/
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
Recursively remove files
提问by JT.
Does anyone have a solution to remove those pesky ._ and .DS_Store files that one gets after moving files from a Mac to A Linux Server?
有没有人有解决方案来删除那些在将文件从 Mac 移动到 Linux 服务器后获得的讨厌的 ._ 和 .DS_Store 文件?
specify a start directory and let it go? like /var/www/html/ down...
指定一个开始目录并让它去?像 /var/www/html/ 下来...
回答by mopoke
cd /var/www/html && find . -name '.DS_Store' -print0 | xargs -0 rm
cd /var/www/html && find . -name '._*' -print0 | xargs -0 rm
回答by Martin Beckett
find . -name "FILE-TO-FIND"-exec rm -rf {} \;
回答by X-Istence
change to the directory, and use:
切换到目录,然后使用:
find . -name ".DS_Store" -print0 | xargs -0 rm -rf
find . -name "._*" -print0 | xargs -0 rm -rf
Not tested, try them without the xargs first!
未经测试,请先在没有 xargs 的情况下尝试它们!
You could replace the period after find, with the directory, instead of changing to the directory first.
您可以将 find 后的句点替换为目录,而不是先更改为目录。
find /dir/here ...
回答by Dave Kirby
You could switch to zsh instead of bash. This lets you use ** to match files anywhere in a directory tree:
您可以切换到 zsh 而不是 bash。这使您可以使用 ** 匹配目录树中任何位置的文件:
$ rm /var/www/html/**/_* /var/www/html/**/.DS_Store
You can also combine them like this:
你也可以像这样组合它们:
$ rm /var/www/html/**/(_*|.DS_Store)
Zsh has lots of other features that bash lacks, but that one alone is worth making the switch for. It is available in most (probably all) linux distros, as well as cygwin and OS X.
Zsh 具有 bash 缺乏的许多其他功能,但仅此一项就值得转换。它在大多数(可能所有)Linux 发行版以及 cygwin 和 OS X 中都可用。
You can find more information on the zsh site.
您可以在zsh 站点上找到更多信息。
回答by OneOfOne
find /var/www/html \( -name '.DS_Store' -or -name '._*' \) -delete
回答by ghostdog74
if you have Bash 4.0++
如果你有 Bash 4.0++
#!/bin/bash
shopt -s globstar
for file in /var/www/html/**/.DS_Store /var/www/html/**/._
do
echo rm "$file"
done
回答by KimKha
Simple command:
简单命令:
rm `find ./ -name '.DS_Store'` -rf
rm `find ./ -name '._'` -rf
Good luck!
祝你好运!
回答by rattray
Newer findutils supports -delete
, so:
较新的 findutils 支持-delete
,因此:
find . -name ".DS_Store" -delete
Add -print
to also get a list of deletions.
添加-print
还可以获得删除列表。
Command will work for you if you have an up-to-date POSIX system, I believe. At least it works for me on OS X 10.8 and works for others who've tested it on macOS 10.12 (Mojave).
我相信,如果您拥有最新的 POSIX 系统,Command 将对您有用。至少它在 OS X 10.8 上适用于我,也适用于在 macOS 10.12 (Mojave) 上测试过的其他人。
Credit to @ephemient in a comment on @X-Istence's post (thought it was helpful enough to warrant its own answer).
在对@X-Istence 的帖子的评论中归功于@ephemient(认为它很有帮助以保证自己的答案)。
回答by Karan Bhalla
A few things to note:
需要注意的几点:
'-delete' is not recursive. So if .TemporaryItems (folder) has files in it, the command fails.
'-delete' 不是递归的。因此,如果 .TemporaryItems(文件夹)中有文件,则命令将失败。
There are a lot of these pesky files created by macs: .DS_Store ._.DS_Store .TemporaryItems .apdisk
macs 创建了很多这些讨厌的文件: .DS_Store ._.DS_Store .TemporaryItems .apdisk
This one command addresses all of them. Saves from running find over and over again for multiple matches.
这一命令解决了所有这些问题。为多个匹配项一遍又一遍地运行查找保存。
find /home/foo \( -name '.DS_Store' -or -name '._.DS_Store' -or -name '._*' -or -name '.TemporaryItems' -or -name '.apdisk' \) -exec rm -rf {} \;
回答by parasrish
Example to delete "Thumbs.db" recursively;
递归删除“Thumbs.db”示例;
find . -iname "Thumbs.db" -print0 | xargs -0 rm -rf
Validate by:
验证方式:
find . -iname "Thumbs.db"
This should now, not display any of the entries with "Thumbs.db", inside the current path.
现在应该不会在当前路径中显示任何带有“Thumbs.db”的条目。