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

0

3 Answers 3

8

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

find ./ -inum 167794 -exec rm {} \;
3
  • 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, 2011 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, 2011 at 15:19
  • 1
    man find advises to escape or quote {}, too. Apr 13, 2011 at 22:21
6

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

find ./ -inum 167794 -delete
0

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

find ./ -inum 167794 -exec rm '{}' ';'
5
  • For every known shell, no masking of the braces is needed. Or earn some points on unix & linux. Apr 13, 2011 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. Apr 13, 2011 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? Apr 13, 2011 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. Apr 13, 2011 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. Apr 13, 2011 at 23:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .