14

Through a boneheaded maneuver on my part, I accidentally created a directory called (for instance) -A, and ended up filling it with files.

I want to delete said directory. I've tried:

rmdir -- -A

but it then tells me that the directory still has files in it. And I can't figure out how to cd into the directory to delete said files.

What should I do to get rid of this troublesome directory?

2

8 Answers 8

29

Use -- on every command.

$ ls -la
total 32
drwxr-xr-x  3 richard  richard  512 Jul 28 15:44 .
drwxr-xr-x  3 root     wheel    512 Jul  6 17:10 ..
$ mkdir -- -A
$ ls -la
total 36
drwxr-xr-x  2 richard  richard  512 Jul 28 15:44 -A
drwxr-xr-x  4 richard  richard  512 Jul 28 15:44 .
drwxr-xr-x  3 root     wheel    512 Jul  6 17:10 ..
$ cd -- -A
$ ls
$ pwd
/home/richard/-A
$ cd ..
$ rm -rf -- -A
$ ls -la
total 32
drwxr-xr-x  3 richard  richard  512 Jul 28 15:44 .
drwxr-xr-x  3 root     wheel    512 Jul  6 17:10 ..
11

You can put ./ in front of the file -- that way rm or rmdir won't behave as though the filename is an option flag.

You can also issue the option -- which tells rm to act as though everything after the -- is a filename and that it should not process any more options. There may be funky older versions of rm that don't obey that, though my zoo of antique unixes has gotten pretty small these days so I can't tell you which ones or if there are versions that don't understand --.

You should get into the habit of putting the ./ in front of names when you're deleting anyhow -- you never know if there is a -r or -rf named file in your directory. You could say that you should always use the -- but I find that the ./ is more natural because it makes explicit that "I want to delete files in this directory, not whatever * happens to glob out to"

4

Rename it and then deal with it normally:

$ mv -- -A foo
$ find foo
$ rm -rf foo
2

rmdir will not delete a directory with anything inside it. "rm -rf" will. rmdir is considerably safer than "rm -rf". Moo's answer is still probably the best.

1
rm -fr ./-A

The above command works for me.

0

A different method would be to delete by inode (this works for files/directories with other characters that are difficult to delete with just rm ).

bash-3.2$ ls -i
4905937 angry-1.gif
5174052 hermes
5172770 make_hosts.pl
bash-3.2$ find . -inum 5172770 -exec rm -f {} \;
bash-3.2$ ls -i
4905937 angry-1.gif
5174052 hermes
bash-3.2$
3
  • the rm will still be passed a -A argument on the commandline and will still try to interpret it as an option, so you still need to prevent it from seeing the leading dash somehow (through double dashes or by putting ./ in front of the rest of the name.
    – chris
    Jul 28, 2009 at 19:10
  • The find will prepend that automatically, since you're finding from '.'. The filename passed in will be './-A', or whatever the file might be called, prepended with ./ and properly escaped for the command.
    – baumgart
    Jul 29, 2009 at 17:07
  • Nit: -exec doesn't escape the name, and doesn't need to; the argument passed via one of the exec* syscalls is the actual name. It is only shell input that needs special chars like space semicolon asterisk etc. quoted to get shell to pass the correct (WITHOUT quotes) actual name. That said, find -exec rm -f {} only works for a file, not a directory as asked for this Q; find -exec rm -rf {} does handle directory and even nonempty directory, but skipping find and just doing rm -rf ./-A is much simpler. Sep 17, 2019 at 3:41
0

find can delete the directory in this way:

find . -type d -iname '-A' -delete
0

I have came across similar situation where i accidentally create -p folder because of the script of creating directory provided by my team member contains special character, on the -p part.

Etc: mkdir –p /home/web/country/pl/temp

mkdir: cannot create directory `/home/web/country/pl/temp': No such file or directory

I found out that -p directory created under directory /home/web/country/

drwxr-x--- 2 web web 4096 Jun 20 19:22 –p/

I tried to delete using above option rm -rf -- -p

It doesn't work, the folder still there (maybe because the -p is a special characters/non ascii)

I manage to delete this folder using below command

rm -r *p

Since I don't have any other folder with *p in their name, so I feel its save to try this.

You must log in to answer this question.

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