deleting file with special character in name

deleting file with special character in name

Post by Joh » Wed, 29 Oct 2003 08:33:59



Through some unfortunate mistyping on my part, I've got a file called
"-C" on my Solaris box.  I've been trying to get rid of it, but every
method I've tried has failed.

I tried
  rm -C
and
  rm \-C
and
  rm "-C"
and
  rm "\-C"

but none of those work.  Can somebody show me a method that will
delete it?  Is there perhaps some way to determine it's inode and use
that to delete it?

thanks,

John.

 
 
 

deleting file with special character in name

Post by William Par » Wed, 29 Oct 2003 08:39:34



> Through some unfortunate mistyping on my part, I've got a file called
> "-C" on my Solaris box.  I've been trying to get rid of it, but every
> method I've tried has failed.

> I tried
>  rm -C
> and
>  rm \-C
> and
>  rm "-C"
> and
>  rm "\-C"

> but none of those work.  Can somebody show me a method that will
> delete it?  Is there perhaps some way to determine it's inode and use
> that to delete it?

rm -- -C
man rm
rm --help

--

Linux solution for data management and processing.

 
 
 

deleting file with special character in name

Post by laura fairhe » Wed, 29 Oct 2003 09:20:12



>Through some unfortunate mistyping on my part, I've got a file called
>"-C" on my Solaris box.  I've been trying to get rid of it, but every
>method I've tried has failed.

>I tried
>  rm -C
>and
>  rm \-C
>and
>  rm "-C"
>and
>  rm "\-C"

>but none of those work.  Can somebody show me a method that will
>delete it?  Is there perhaps some way to determine it's inode and use
>that to delete it?

Hello John,

Well all of the first 3 methods are exactly the same - in each case
rm gets a first argument of -C. The quoting you did with the backslash
and then the double quotes in the 2nd and 3rd tries is something that
is dealt with by the shell and its used to protect characters that
might be otherwise interpretted by the shell specially; but neither
the hyphen or the C are special so the end result is no effect.

Of course, because the argument starts with a hyphen 'rm' actually
interprets that and thinks its an option. The problem is not stopping
the shell interpret the characters, it is just to stop 'rm' interpretting
that hyphen. There are two ways to do this;

(i)  rm ./-C

This is very common and in fact is one reason why you see ./file
a lot to refer to a file in the current directory - it's just a good
habit because most utilities have options and any filename that
begins with a hyphen would otherwise give you the same problem you are
getting here.

(ii)  rm -- -C

In this case '--' tells 'rm' that it is the last option argument
so it knows only files will go afterwards. '--' is standard POSIX
and will work with almost every command line utility however it
may not be supported on a non-POSIX system and often shell scripters
would prefer not to have '--' all over the place in every command
so this is why you might see things like;

for file in ./*
do
  utilitity $file
done

In your last case the shell passes 'rm' a single argument \-C
and rm just can't find the file (there is no file called BACKSLASH HYPHEN C)

btw: You can get the shell to show you what is being executed by
issuing a 'set -x'

$ set -x
$ rm -C
+ rm -C
rm: invalid option -- C
Try `rm --help' for more information.
$ rm \-C
+ rm -C
rm: invalid option -- C
Try `rm --help' for more information.
$ rm "-C"
+ rm -C
rm: invalid option -- C
Try `rm --help' for more information.
$ rm "\-C"
+ rm \-C
rm: cannot remove `\-C': No such file or directory
$

'rm' doesn't support _any_ kind of quoting (only the shell) in fact
if it did there would be problems dealing with arbitrary files
and commands like; rm ./*  could fail (if some of the files contained
backslashes)

bye for now
laura

Quote:

>thanks,

>John.

--

 
 
 

deleting file with special character in name

Post by Alan Conno » Wed, 29 Oct 2003 09:29:05




>> Through some unfortunate mistyping on my part, I've got a file called
>> "-C" on my Solaris box.  I've been trying to get rid of it, but every
>> method I've tried has failed.

>> I tried
>>  rm -C
>> and
>>  rm \-C
>> and
>>  rm "-C"
>> and
>>  rm "\-C"

>> but none of those work.  Can somebody show me a method that will
>> delete it?  Is there perhaps some way to determine it's inode and use
>> that to delete it?

> rm -- -C
> man rm
> rm --help

I thought that only worked with GNU rm.

--
Alan C

 
 
 

deleting file with special character in name

Post by Charles Dem » Wed, 29 Oct 2003 10:21:01




>Through some unfortunate mistyping on my part, I've got a file called
>"-C" on my Solaris box.  I've been trying to get rid of it, but every
>method I've tried has failed.

>I tried
>  rm -C
>and
>  rm \-C
>and
>  rm "-C"
>and
>  rm "\-C"

>but none of those work.  Can somebody show me a method that will
>delete it?  Is there perhaps some way to determine it's inode and use
>that to delete it?

Read the FAQ

rm ./-C

Chuck Demas

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.

 
 
 

deleting file with special character in name

Post by Fred L. Kleinschmid » Wed, 29 Oct 2003 10:32:01



> Through some unfortunate mistyping on my part, I've got a file called
> "-C" on my Solaris box.  I've been trying to get rid of it, but every
> method I've tried has failed.

> I tried
>   rm -C
> and
>   rm \-C
> and
>   rm "-C"
> and
>   rm "\-C"

> but none of those work.  Can somebody show me a method that will
> delete it?  Is there perhaps some way to determine it's inode and use
> that to delete it?

> thanks,

> John.

Besides the methods already explained by others, you can use the "-i"
option to force rm to ask you about each file:   rm -i *C

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94  (206)544-5225

 
 
 

deleting file with special character in name

Post by laura fairhe » Wed, 29 Oct 2003 11:05:37






>>> Through some unfortunate mistyping on my part, I've got a file called
>>> "-C" on my Solaris box.  I've been trying to get rid of it, but every
>>> method I've tried has failed.

>>> I tried
>>>  rm -C
>>> and
>>>  rm \-C
>>> and
>>>  rm "-C"
>>> and
>>>  rm "\-C"

>>> but none of those work.  Can somebody show me a method that will
>>> delete it?  Is there perhaps some way to determine it's inode and use
>>> that to delete it?

>> rm -- -C
>> man rm
>> rm --help

>I thought that only worked with GNU rm.

Hi Alan,

This is actually part of the POSIX specification and should
be supported by every utility unless otherwise stated.

At the current status I'm not sure that it is strictly _required_
but obviously if not now it will be in the future,

[ xbd chapter 3 - Utility syntax guidelines ]

   Guideline 10:
          The argument -- should be accepted as a delimiter indicating
          the end of options. Any following arguments should be treated
          as operands, even if they begin with the '-' character. The --
          argument should not be used as an option or as an operand.
:
:
:

Unless of course you meant the '--help' which probably is only GNU/FSF ;)

seeya
laura

Quote:

>--
>Alan C

--

 
 
 

deleting file with special character in name

Post by laura fairhe » Wed, 29 Oct 2003 11:16:21




>> Through some unfortunate mistyping on my part, I've got a file called
>> "-C" on my Solaris box.  I've been trying to get rid of it, but every
>> method I've tried has failed.

>> I tried
>>   rm -C
>> and
>>   rm \-C
>> and
>>   rm "-C"
>> and
>>   rm "\-C"

>> but none of those work.  Can somebody show me a method that will
>> delete it?  Is there perhaps some way to determine it's inode and use
>> that to delete it?

>> thanks,

>> John.

>Besides the methods already explained by others, you can use the "-i"
>option to force rm to ask you about each file:   rm -i *C

Hello Fred,

That still won't work - the shell expands '*C' into '-C' so
passes exactly the same thing to 'rm' except with an additional
(first) option argument.

'rm' will still think the -C is an option, since options only stop
being interpretted after a '--' (some utilities will also stop
after the first filename but GNU-rm doesn't)

bestwishes
laura

Quote:

>--
>Fred L. Kleinschmidt
>Boeing Associate Technical Fellow
>Technical Architect, Common User Interface Services
>M/S 2R-94  (206)544-5225

--

 
 
 

deleting file with special character in name

Post by Alan Conno » Wed, 29 Oct 2003 12:29:05






<snip>

Quote:>>> rm -- -C
>>> man rm
>>> rm --help

>>I thought that only worked with GNU rm.

> Hi Alan,

> This is actually part of the POSIX specification and should
> be supported by every utility unless otherwise stated.

> At the current status I'm not sure that it is strictly _required_
> but obviously if not now it will be in the future,

> [ xbd chapter 3 - Utility syntax guidelines ]

>    Guideline 10:
>           The argument -- should be accepted as a delimiter indicating
>           the end of options. Any following arguments should be treated
>           as operands, even if they begin with the '-' character. The --
>           argument should not be used as an option or as an operand.
>:
>:
>:

> Unless of course you meant the '--help' which probably is only GNU/FSF ;)

arh arh arh :-)

Quote:> seeya
> laura

Thanks again laura. You wouldn't believe how many posts of yours I
have in my bash docs dir........

> --


Isn't a woman sysadmin called a "sysadminx" ?

<img src="../images/the_devil_on_his_tail.png" alt="[ aging hippy running
unceremoniously for the nearest heavy cover ]">

:-)

--
Alan C

 
 
 

deleting file with special character in name

Post by Peter J. Ackl » Wed, 29 Oct 2003 16:34:05



> Is there perhaps some way to determine it's inode and use that
> to delete it?

As others have pointed out, you don't need to get the inode for
this.  However, if you accidentally create a file with a really
weird name, perhaps a name you can't even type on the keyboard,
the inode method might be useful though, so here is a way

Do this on the directory containing the file

   ls -lib DIR

to see the inode number of the file, and then do

   find DIR -inum NNN -exec rm {} \;

Peter

--

 
 
 

deleting file with special character in name

Post by Master Web Surfe » Wed, 29 Oct 2003 23:32:57


[This followup was posted to comp.unix.shell]


Quote:> Through some unfortunate mistyping on my part, I've got a file called
> "-C" on my Solaris box.  I've been trying to get rid of it, but every
> method I've tried has failed.

> I tried
>   rm -C
> and
>   rm \-C
> and
>   rm "-C"
> and
>   rm "\-C"

> but none of those work.  Can somebody show me a method that will
> delete it?  Is there perhaps some way to determine it's inode and use
> that to delete it?

> thanks,

> John.

Try this :

rm ./-C

 
 
 

deleting file with special character in name

Post by Fred L. Kleinschmid » Thu, 30 Oct 2003 02:29:58





> >> Through some unfortunate mistyping on my part, I've got a file called
> >> "-C" on my Solaris box.  I've been trying to get rid of it, but every
> >> method I've tried has failed.

> >> I tried
> >>   rm -C
> >> and
> >>   rm \-C
> >> and
> >>   rm "-C"
> >> and
> >>   rm "\-C"

> >> but none of those work.  Can somebody show me a method that will
> >> delete it?  Is there perhaps some way to determine it's inode and use
> >> that to delete it?

> >> thanks,

> >> John.

> >Besides the methods already explained by others, you can use the "-i"
> >option to force rm to ask you about each file:   rm -i *C

> Hello Fred,

> That still won't work - the shell expands '*C' into '-C' so
> passes exactly the same thing to 'rm' except with an additional
> (first) option argument.

> 'rm' will still think the -C is an option, since options only stop
> being interpretted after a '--' (some utilities will also stop
> after the first filename but GNU-rm doesn't)

> bestwishes
> laura

> >--
> >Fred L. Kleinschmidt
> >Boeing Associate Technical Fellow
> >Technical Architect, Common User Interface Services
> >M/S 2R-94  (206)544-5225

> --


Ah, yes, since "-" is syntactically ahead all letters and numbers.
ONe could use   "rm asjklhfasjlhkhjkadsf -C"   where the first argument
is just a bunch of junk (known not to be the name of an existing file).
In this case, -C will be interpreted as a file name.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94  (206)544-5225
 
 
 

deleting file with special character in name

Post by laura fairhe » Thu, 30 Oct 2003 04:15:52






>> >> Through some unfortunate mistyping on my part, I've got a file called
>> >> "-C" on my Solaris box.  I've been trying to get rid of it, but every
>> >> method I've tried has failed.

>> >> I tried
>> >>   rm -C
>> >> and
>> >>   rm \-C
>> >> and
>> >>   rm "-C"
>> >> and
>> >>   rm "\-C"

>> >> but none of those work.  Can somebody show me a method that will
>> >> delete it?  Is there perhaps some way to determine it's inode and use
>> >> that to delete it?

>> >> thanks,

>> >> John.

>> >Besides the methods already explained by others, you can use the "-i"
>> >option to force rm to ask you about each file:   rm -i *C

>> Hello Fred,

>> That still won't work - the shell expands '*C' into '-C' so
>> passes exactly the same thing to 'rm' except with an additional
>> (first) option argument.

>> 'rm' will still think the -C is an option, since options only stop
>> being interpretted after a '--' (some utilities will also stop
>> after the first filename but GNU-rm doesn't)

>> bestwishes
>> laura

>> >--
>> >Fred L. Kleinschmidt
>> >Boeing Associate Technical Fellow
>> >Technical Architect, Common User Interface Services
>> >M/S 2R-94  (206)544-5225

>> --

>Ah, yes, since "-" is syntactically ahead all letters and numbers.
>ONe could use   "rm asjklhfasjlhkhjkadsf -C"   where the first argument
>is just a bunch of junk (known not to be the name of an existing file).
>In this case, -C will be interpreted as a file name.

It may do on some systems however, for example, GNU-rm interprets options
that are given after filenames;

$ rm --version
rm (GNU fileutils) 4.0
$ rm whatever -C
rm: invalid option -- C
Try `rm --help' for more information.
$

Actually this behaviour is condoned by POSIX -

[ xbd chapter 12 - "Utility Syntax Guidlines" ]

   Guideline 9:
          All options should precede operands on the command line.

But I don't think it's actually _illegal_ yet.  As far as I can see quickly
browsing through the spec (since they turned it into 4000 HTML files only and
took out all the section numbering I'm adverse to wasting my time with it)
it's only 'getopt()' that actually has to obey this guideline, otherwise
it seems to be only a recommendation.

bestwishes
laura

Quote:>--
>Fred L. Kleinschmidt
>Boeing Associate Technical Fellow
>Technical Architect, Common User Interface Services
>M/S 2R-94  (206)544-5225

--

 
 
 

deleting file with special character in name

Post by Stephane CHAZELA » Thu, 30 Oct 2003 04:27:59


2003/10/28, 17:29(+00), Fred L. Kleinschmidt:
[...]

Quote:> Ah, yes, since "-" is syntactically ahead all letters and numbers.
> ONe could use   "rm asjklhfasjlhkhjkadsf -C"   where the first argument
> is just a bunch of junk (known not to be the name of an existing file).
> In this case, -C will be interpreted as a file name.

Why would it make any difference that "-" is ahead of any
character? rm doesn't sort its arguments.

rm asjklhfasjlhkhjkadsf -C

With GNU rm, I get

$ rm azeadasdasd -C
rm: invalid option -- C
Try `rm --help' for more information.

Generally GNU utilities allow options anywhere (except in
argv[0]) unless you use "--".

So, the correct answers definitely are:

rm -- -C
or
rm ./-C
if your rm doesn't support "--".

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]

 
 
 

deleting file with special character in name

Post by Alan Conno » Thu, 30 Oct 2003 04:59:05



> 2003/10/28, 17:29(+00), Fred L. Kleinschmidt:
> [...]
>> Ah, yes, since "-" is syntactically ahead all letters and numbers.
>> ONe could use   "rm asjklhfasjlhkhjkadsf -C"   where the first argument
>> is just a bunch of junk (known not to be the name of an existing file).
>> In this case, -C will be interpreted as a file name.

> Why would it make any difference that "-" is ahead of any
> character? rm doesn't sort its arguments.

> rm asjklhfasjlhkhjkadsf -C

> With GNU rm, I get

> $ rm azeadasdasd -C
> rm: invalid option -- C
> Try `rm --help' for more information.

> Generally GNU utilities allow options anywhere (except in
> argv[0]) unless you use "--".

> So, the correct answers definitely are:

> rm -- -C
> or
> rm ./-C
> if your rm doesn't support "--".

> --
> Stphane                      ["Stephane.Chazelas" at "free.fr"]

You can also do

set AAAA BBBBB `ls`

find the variable that belongs to -C and do

rm "${n}"

--
Alan C