在 Linux 上使用 find 命令查找和删除带有空格的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6242026/
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
find and remove files with space using find command on Linux
提问by Ahmad Azimi
I'm trying to remove all thumbs.db
files in a Windows partition using findcommand in Ubuntu:
我正在尝试thumbs.db
使用Ubuntu 中的find命令删除Windows 分区中的所有文件:
find . -iname "*.db"|while read junk;do rm -rfv $junk;done
But it's not working for me and nothing happens! I think I found the problem, the white spaces in directory names!
但这对我不起作用,什么也没有发生!我想我发现了问题,目录名称中的空格!
I did this trick to remove my junk files before on previous version of Ubuntu but now on latest version of Ubuntu I can't.
我之前在以前版本的 Ubuntu 上使用这个技巧删除了我的垃圾文件,但现在在最新版本的 Ubuntu 上我不能。
Is there any bug in my command?
我的命令中是否有任何错误?
采纳答案by Chris Jester-Young
I'd do it this way:
我会这样做:
find . -iname 'thumbs.db' -exec rm -rfv {} +
This way, it still works even if your directories contain whitespace in their names.
这样,即使您的目录名称中包含空格,它仍然有效。
回答by Pascal Wittmann
The code looks good and works on arch and debian. Maybe there are no files matching "*.db"?
代码看起来不错,适用于 arch 和 debian。也许没有匹配“*.db”的文件?
As a sidenote: I might not be a good idea to delete all files with the suffix ".db", because you can accidently delete other files than "Thumbs.db"
旁注:删除所有带有“.db”后缀的文件可能不是一个好主意,因为您可能会意外删除“Thumbs.db”以外的其他文件
回答by Johnsyweb
I'm not sure why you're using while
.
我不确定你为什么使用while
.
find . -iname 'thumbs.db' -exec rm -rfv {} \;
...should suffice (and only delete the files you want to, not any BDB files that may be laying around).
...应该就足够了(并且只删除您想要删除的文件,而不是可能放置的任何 BDB 文件)。
回答by Ozair Kafray
First check if the first part of your command, that is:
首先检查您的命令的第一部分,即:
find . -iname "*.db"
找 。-iname "*.db"
is returning anything.
正在返回任何东西。
If it does then you can use xargs
as follows to accomplish your task:
如果是,那么您可以使用xargs
以下方法来完成您的任务:
find . -iname "*.db" | xargs rm -rfv
找 。-iname "*.db" | xargs rm -rfv
UPDATE: From comments, this is unsafe, specially if there are spaces in directory/file names. You will need to use -print0
/ xargs -0
to make it safe.
更新:根据评论,这是不安全的,特别是如果目录/文件名中有空格。您将需要使用-print0
/xargs -0
使其安全。
回答by Michael
just to throw this out there
只是想把它扔出去
find . -name "*.pyc" -delete