开发者

find and remove files with space using find command on Linux

I'm trying to remove all thumbs.db files in a Windows partition using fi开发者_如何学JAVAnd 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 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.

Is there any bug in my command?


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.


just to throw this out there

find . -name "*.pyc" -delete


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


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"


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜