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?
Use -- on every command.
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"
Rename it and then deal with it normally:
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.
The above command works for me.
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 ).
find can delete the directory in this way:
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.
I was trying to delete a directory named
-p
. @Moo's answer achieved what I was not able to withrm ./-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:
find
command using the-exec rm -irf
argument for a file or-exec rm -ifdr
for a directory.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: