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?

ewwhite
  • 194,921
  • 91
  • 434
  • 799
Kate Bertelsen
  • 243
  • 2
  • 6
  • 5
    Luckily the directory wasn't named '-rf *'. – user1686 Jul 28 '09 at 17:45
  • Same on [stackoverflow](https://stackoverflow.com/questions/706196/how-to-remove-files-starting-with-double-hyphen) and [unix](https://unix.stackexchange.com/questions/1519/how-do-i-delete-a-file-whose-name-begins-with-hyphen-a-k-a-dash-or-minus). – Dorian May 31 '17 at 18:04

9 Answers9

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 ..
Moo
  • 2,225
  • 19
  • 23
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"

chris
  • 11,784
  • 6
  • 41
  • 51
4

Rename it and then deal with it normally:

$ mv -- -A foo
$ find foo
$ rm -rf foo
David Mackintosh
  • 14,223
  • 6
  • 46
  • 77
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.

David Pashley
  • 23,151
  • 2
  • 41
  • 71
1
rm -fr ./-A

The above command works for me.

q0987
  • 81
  • 5
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.

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$
baumgart
  • 2,423
  • 18
  • 17
  • 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 '09 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 '09 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. – dave_thompson_085 Sep 17 '19 at 03:41
0

find can delete the directory in this way:

find . -type d -iname '-A' -delete
ThorstenS
  • 3,084
  • 18
  • 21
-1

I was trying to delete a directory named -p. @Moo's answer achieved what I was not able to with rm ./-p, which is widely available throughout many web pages after a Google search.

Another method is to use the inode number. I used the method described here:

https://www.cyberciti.biz/tips/delete-remove-files-with-inode-number.html

In short, use the following commands:

  1. To display the file or directory inode number.
ls -il
  1. Use the find command using the -exec rm -irf argument for a file or -exec rm -ifdr for a directory.
find . -inum <inode-number> -exec rm -irf {} \;
find . -inum <inode-number> -exec rm -irdf {} \;

Make sure you use the -f within the flag, otherwise bash will complain of not being able to find the directory or file.

Tested on:

Distributor ID: Ubuntu
Description:    Ubuntu 16.04.6 LTS
Release:        16.04
Codename:       xenial
  • 1
    This is nearly duplicate of baumgart's answer from 10 years ago, but you don't need `rm -d` ever and usually not `-f` when invoking from `find`, but you do need `-r` for a directory and _don't_ need it for a file. Even when corrected this method is still clumsy and inferior. For a nonempty directory just do `rm -fr ./-p` -- that's r for a directory, and f depending on the permission settings. – dave_thompson_085 Sep 17 '19 at 03:43
  • 1
    Both methods were already mentioned in previous answers. – RalfFriedl Sep 17 '19 at 05:12