22

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

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

5
  • 1
    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). Apr 7, 2014 at 11:40
  • 11
    Gotta ask why you'd want such a directory. It would be a PITA to use. Apr 7, 2014 at 11:44
  • 3
    @DanielRHicks: I created this folder by mistake, but do not know how to delete it.
    – Jichao
    Apr 7, 2014 at 14:19
  • 4
    Shouldn't, then, the question be "How to delete a directory named '-p' under Linux with Bash"?
    – chepner
    Apr 7, 2014 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, 2014 at 16:41

2 Answers 2

40

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.

1
  • 3
    Awesome. This is really good to know
    – Bruno9779
    Apr 7, 2014 at 16:53
18

mkdir ./-p

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

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