There's a lot of stuff about this on the Internet, but most of the examples there are contrived. How does one delete files that are really stubborn? e.g.,

$ find ./ -inum 167794
$ find ./ -inum 167794 -exec rm \"{}\" \;
rm: cannot lstat `"./\037\340\025K\021\004\310\377\377\377\377@\020\002"': Invalid or incomplete multibyte or wide character


Try removing the escaped double quotes. I believe rm thinks those are part of the filename.

find ./ -inum 167794 -exec rm {} \;
  • d'oh. Good spot. I never use unquoted variables in rm commands. I never noticed that find seems to be able to handle them fine. This actually works... weird... is it safe? touch important; touch "myfile -f important"; touch myfile; find "myfile -f important" -exec rm {} \; – mgjk Apr 13 '11 at 14:40
  • 3
    It's safe. You quote arguments to protect them from being touched by the shell. In this case, find is passing arguments directly to rm and they won't be interpreted/expanded by the shell. – MikeyB Apr 13 '11 at 15:19
  • 1
    man find advises to escape or quote {}, too. – sam hocevar Apr 13 '11 at 22:21

Better way with modern find (version 4.2.3 or later):

find ./ -inum 167794 -delete

If you want safe quoting for every shell I know, use this:

find ./ -inum 167794 -exec rm '{}' ';'
  • For every known shell, no masking of the braces is needed. Or earn some points on unix & linux. – user unknown Apr 13 '11 at 18:13
  • @user unknown: Or you could just read man find: Both of [{} and ;] might need to be escaped or quoted to protect them from expansion by the shell. – sam hocevar Apr 13 '11 at 22:19
  • If you had visited my link, you would have seen an emphasized citation citation of that. The text from the man page raised my interest, and I'm waiting since one month for somebody, who can name a current shell, where the argument is valid. might need is the keyword. Might or might not. Maybe I spend a bounty? – user unknown Apr 13 '11 at 22:36
  • 1
    @user unknown: I'm really sorry, I mistook the tone of your message and actually thought it meant I should earn points in order to be listened to. My apologies. That said, I tend to quote anything using { } because I don't want to have of think whether it is likely to get expanded. I agree {} is kinda special and I haven't met a shell that messed with it either. – sam hocevar Apr 13 '11 at 22:50
  • Ah no, I'm sorry for the ambiguous tone. I don't know how old the entry of find is, and don't know any shell, beside bash a bit, nor much OS' beside Linux. So I'm searching for the reason of that entry, because first I thought, it should be masked (from the manpage, and examples) and told everybody to mask it, got later curious, when seeing diverging examples which worked. – user unknown Apr 13 '11 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.