8

I've accidentally created a file called

> option[value='2016']

How can I delete it?

My attempts:

$ rm "> option[value='2016']"
rm: cannot remove ‘> option[value='2016']’: No such file or directory
$ rm \> o*
rm: cannot remove ‘>’: No such file or directory
rm: cannot remove ‘o*’: No such file or directory
$ rm `> o*`                                                                               
rm: missing operand
Try 'rm --help' for more information.
$ rm \> option*
rm: cannot remove ‘>’: No such file or directory
rm: cannot remove ‘option*’: No such file or directory
$ rm '\> option*'                                                                         
rm: cannot remove ‘\\> option*’: No such file or directory
$
$ rm "\> option*"                                                                         
rm: cannot remove ‘\\> option*’: No such file or directory

File listing:

HAPPY_PLUS_OPTIONS/
o*
op*
> option[value='2016']
> option[value='ALFA ROMEO']
README.md
rspec_conversions/
.rubocop.yml
SAD/
SAD_PLUS_OPTIONS/

5 Answers5

16

another option

ls -i 

which give (with proper inode value)

5233 > option[value='2016']   5689 foo

then

find . -inum 5233 -delete

optionnaly (to preview)

find . -inum 5233 -print

you can also add -xdev if there is another filesystem beneath .

Archemar
  • 31,554
9

You can also use the "--" option which according to man:

 The rm command uses getopt(3) to parse its arguments, which allows it to
 accept the `--' option which will cause it to stop processing flag options at
 that point.  This will allow the removal of file names that begin with a dash
 (`-').  For example:
       rm -- -filename

So I tried:

touch -- "> option[value='2016']"

And removed it with:

rm -- "> option[value='2016']"

Easiest way to check if the filename was correctly entered:

rm -- ">[tab]

And let auto-complete do the job.

PS: As tempting as it sounds, don't create a file name "-rf *". Bad things may happen.

-rw-r--r--    1 stephan  staff      0 Sep 13 14:11 -rf *

Always use "-i" to be safe.

iMac:~ stephan$ rm -i -- "-rf *"
remove -rf *? Y
8

The initial problem was a leading space, thus

rm " > option[value='2016']"
    ^ here

works.

Updated the question to be about files starting with > etc.

3

For an interactive approach (often safer):

If there are some special named files in current directory.

You can use rm ./ and then TabTab to list files and then you can select the file and delete it.

Shellmode
  • 101
1

For rm, there's nothing magical about >. You only need to make sure the angle bracket gets to it (=prevent the shell from interpretting it as a redirection).

> "> option[value='2016']"  #create it
rm "> option[value='2016']" #remove it

#remove all files in the current directory that have > in them
rm -- {,.}*\>*                 

If you're on a sensible modern system, you should be able to get properly escaped names with tab completion.

Petr Skocik
  • 28,816