6

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
./àKÈÿÿÿÿ@
$ 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
mgjk
  • 854
  • 3
  • 9
  • 19

3 Answers3

8

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

find ./ -inum 167794 -exec rm {} \;
glenn jackman
  • 4,320
  • 16
  • 19
  • 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
6

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

find ./ -inum 167794 -delete
womble
  • 95,029
  • 29
  • 173
  • 228
METAJIJI
  • 161
  • 1
  • 3
0

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

find ./ -inum 167794 -exec rm '{}' ';'
sam hocevar
  • 103
  • 5
  • For every known shell, no masking of the braces is needed. Or earn some points on [unix & linux](http://unix.stackexchange.com/q/8647/4485). – 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