Batch find & remove files on linux using find comm

2019-03-11 09:28发布

Hi everybody I'm trying to remove all thumbs.db files in a Windows partition using find command in ubuntu:

find . -iname "*.db"|while read junk;do rm -rfv $junk;done

But it's not working for me and nothing happens! 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. Is there any bug in my command?

5条回答
beautiful°
2楼-- · 2019-03-11 09:38

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.

查看更多
聊天终结者
3楼-- · 2019-03-11 09:41

First check if the first part of your command, that is:

find . -iname "*.db"

is returning anything.

If it does then you can use xargs as follows to accomplish your task:

find . -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.

查看更多
叼着烟拽天下
4楼-- · 2019-03-11 09:51

just to throw this out there

find . -name "*.pyc" -delete
查看更多
做自己的国王
5楼-- · 2019-03-11 09:52

I'm not sure why you're using 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).

查看更多
Fickle 薄情
6楼-- · 2019-03-11 09:59

The code looks good and works on arch and debian. Maybe there are no files matching "*.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"

查看更多
登录 后发表回答