Batch find & remove files on linux using find comm

2019-03-11 09:17发布

问题:

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?

回答1:

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.



回答2:

just to throw this out there

find . -name "*.pyc" -delete


回答3:

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



回答4:

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"



回答5:

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.