Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

In Bash, how could I create a directory named -p?

mkdir -p failed.
mkdir "-p" failed.
mkdir "\-p" failed.
mkdir \-p failed.

share|improve this question

marked as duplicate by Daniel Beck Apr 7 '14 at 20:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Note that all of these are attempting to escape the parameter for bash. Since it's the same escaped as not escaped, all of these except the third will just send the string "-p" to mkdir, which will parse it as an option. The option parsing isn't done in bash, so any way around this (described in the answers below) is a feature of mkdir (or rather, a lack of bug). – Score_Under Apr 7 '14 at 11:40
Gotta ask why you'd want such a directory. It would be a PITA to use. – Daniel R Hicks Apr 7 '14 at 11:44
@DanielRHicks: I created this folder by mistake, but do not know how to delete it. – Jichao Apr 7 '14 at 14:19
Shouldn't, then, the question be "How to delete a directory named '-p' under Linux with Bash"? – chepner Apr 7 '14 at 16:15
@chepner: Because they are same problems actually. I tried to delete the directory with rmdir but failed with same errors. – Jichao Apr 7 '14 at 16:41
up vote 40 down vote accepted

Most utilities (all POSIX compliant ones except for test and echo) support an "end of options" option --, so you could run:

mkdir -- -p

This is especially useful when renaming or removing files that could potentially start with a dash. In scripts you should always use

mv -- "$filename"

instead of a plain mv "$filename" or, even worse, an unquoted filename.

share|improve this answer
Awesome. This is really good to know – Bruno9779 Apr 7 '14 at 16:53

mkdir ./-p

Keep in mind that most other programs would need to use the same "trick".

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.