-1

I am trying to remove a directory that has the name --crm. When I do

rm -f -R '--crm'

I get the following error:

rm: illegal option -- -
usage: rm [-f | -i] [-dPRrvW] file ...
   unlink file

I have also tried

rm -f -R *crm

but the directory is not removed.

Any clue how can I do that?

  • 1
    The man page for rm that ships with the OS has a section that describes exactly how to resolve your issue. Type man rm in a terminal and read. – yoonix Jun 06 '16 at 20:25
  • @yoonix I found your comment useful. That's why I upvoted it. You could have answered the question with the actual answer, if you wanted to earn some more reputation points. Are you going to do it so that I can accept that or not? – p.matsinopoulos Jun 06 '16 at 21:58
  • 1
    I didn't post it as an answer because this is really a basic UNIX / Linux question. I flagged this question as off-topic as I feel it should be elsewhere as this really isn't a question about managing servers in a business / production environment. That's only my opinion though, I'm glad you got the information you needed. – yoonix Jun 06 '16 at 23:45

1 Answers1

2

TBH this is probably more suited to one of the other .SE's, either Unix &Linux, Ask Different, or even SuperUser. Which is why I assume you've been downvoted. That said...

The Short Answer

The answer is rm -r ./--crm, or better yet, rmdir ./--crm.


The Real Answer

On OS X you're usually using Bash in the terminal, which is important to know: which shell you use helps determine the syntax of the basic statements that let you manipulate files and directories, which includes renaming, removing, or calling on the kernel to execute them.

Along with man rm, take a few days to read through man bash and learn some of the concepts there.

Aside from this, it's a convention for most Unix command-line programs to run with specific arguments which follow the command. Some are obvious and work by passing data to the program. For instance, by running rm myfile, you call the kernel to run the unlinking program on the file myfile. Other arguments act like a set of switched to turn certain options on or off. These, sometimes known as 'flags', instead of passing data control the behavior of the program itself. For instance, rm -h will remove nothing, but instead prints a short block of helpful text.

So the problem with removing a file with an unusual name, is that the program doesn't know how to interpret it. Throwing characters at it doesn't help either, and can get you into trouble.

Using Bash: Why It Didn't Work

Bash (your shell) interprets anything in single quotes literally, by stripping the quotes and taking the contents as-is. So by running rm '--crm', you're effectively running the same thing as rm --crm. Bash specifically treats * as a special character, doing something we call 'globbing': before the programs you called is run, Bash will expand the argument to every matching pathname you specified. In effect, if you have in your directory

~$ ls
--crm   dummy.txt       file.crm

and then run rm *crm, Bash will first expand this, and then run the expanded version:

~$ rm --crm file.crm

...so you run the risk of destroying all your files ending in 'crm', plus it simply interprets the first directory as a flag again.

Using Bash: What Does Work

What Bash needs to know, and all it needs to know, in order to treat your directory as a file to be removed, is what rm always needs: an explicit, fully qualified pathname. For most normal files, like myfile above, Bash is smart enough to know you normally mean the current working directory— the one you can change into, or out of, with cd. There are two special characters devoted to helping manage this. cd .. for instance will take you to the directory 'above' your current one. If you're in /Users/foobar, and you run cd .., you'll move to /Users. Similarly, there's also the character denoting the current directory, which is simply .. Running cd . will take you... To the directory you're already in. Seems useless, doesn't it?

But this gives us a method of telling Bash exactly where your unfortunately-named directory lives: in the current directory . lives the file --crm, so you can specify it, and remove it, using rm ./--crm — well, specifically this is a directory, and so you have to tell rm to descend and recurse through the directory, and remove everything it finds there, first. You do this with the -r flag: hence, rm -r ./--crm.

A final caveat: now that I've tried to explain all this, I hope you also understand the serious danger in running rm -r -f /. You should never use the -f flag unless you know what you're doing.

Ryder
  • 284
  • 2
    The other way to do it is to use -- which is the common way (especially with GNU utilities) to signify that it is the end of the standard arguments to the command. So, an unworking rm -crm becomes rm -- -crm. Multiple files will work with that too, rm -- -crm -foo ---whateverelse. – yoonix Jun 07 '16 at 00:18
  • @yoonix ah, POSIX. What would we do without you? Thanks for the addition, I've just never gotten used to that syntax. – Ryder Jun 07 '16 at 00:27
  • There is another way, such as using python, and import os, then use python's os library to remove the directory. – Tom O'Connor Jun 07 '16 at 18:42
  • This is weird; who moved the question here from Server Fault? – Ryder Jun 08 '16 at 20:15