Removing weird file names

It’s my own inability to refrain from experimenting that causes this problem so please bear with me. I have file called: -ioP

This just happens to be the options I was using for a grep command when things went south. Anyway, none of these will remove the file:

rm ‘-ioP’
rm -ioP
rm \-ioP
rm ‘-ioP’

and perhaps a few others I tried. Does anyone have a trick or two for weird files like this?

rm ./-ioP

That worked, thanks!

I hope you understand why. ./ is not an escape code. It’s just a way of making sure that the first character of the argument is not -.

ken yap wrote:
> I hope you understand why. ./ is not an escape code. It’s just a way of
> making sure that the first character of the argument is not -.

Q: would

rm ?ioP

do it also?

however, if i were gonna do that i’d first do

ls ?ioP

to see if maybe their might also be in that directory some files i
wanted to keep, like:

pioP
PioP
?ioP
ioP
etc

or do

rm -i ?ioP

and expect to have to answer ‘yes’ once or more…


palladium

The answer depends on whether ? substitution happens before grep sees the argument. What do you think?

Incidentally another way of doing it is:

rm – -ioP

where the – signifies end of options and after that - is not significant as the first character. And both methods are explained in the man page for rm.

ken yap wrote:
> The answer depends on whether ? substitution happens before grep sees
> the argument. What do you think?

hmmmmm…(see, i’m not a real guru and i therefore didn’t know) does
grep have a chance to get in the act if the following is issued:

rm ?ioP

are you saying that the rm command (“behind the scenes”) first calls
grep to filter the list to act on and then performs the remove
actions?? interesting…

> Incidentally another way of doing it is:
>
> rm – -ioP

thanks, read that and promptly forgot it…
(maybe it will ‘stick’ now)


palladium

@palladium: the ‘grep’ probably was from the original command. I’ve done ‘greps’ redirected to a filename which was based on the expected result of the ‘grep’. One result was a filename beginning with “-” since there were no results from ‘grep’. IIRC I was able to move the -file to a new folder, then removed the folder by force( too lazy for ‘man rm’, thinking I knew all that…).

Sorry, I meant rm, not grep, in that command.

No, the shell expands the ?ioP to -ioP and this is what rm sees, and it’s as you typed -ioP to rm and ioP are treated as options.

having just reviewed the last few i am now more confused than ever…

i think the answer to my question would

rm ?ioP

do the same as your

rm ./-ioP

would be "yes, if used alone (as did the OP) and not pipelined in with
grep (or other commands) which might jumble it…or, in other words
your would always work (and is therefor the better way)

am i right? and i will say: thanks for the better way which will
always work!!


palladium

No, as I said, the shell will expand ?ioP to -ioP and this is what rm will see and treat the filename as options rather than a file to remove. Try this:

touch ./-i foo
rm ?i foo

HA!! AH!! now i get it…sorry for being so thick…

and thanks for your patience…


palladium