# Dir.mkdir api

**URL:** <https://forum.crystal-lang.org/t/dir-mkdir-api/4326>\
**Category:** Crystal Contrib\
**Created:** [February 1, 2022, 10:15pm UTC](https://forum.crystal-lang.org/t/dir-mkdir-api/4326 "2022-02-01T22:15:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wontruefree](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/wontruefree/32/1467_2.png) [@wontruefree](https://forum.crystal-lang.org/u/wontruefree)\
**Post date:** [February 1, 2022, 10:15pm UTC](https://forum.crystal-lang.org/t/dir-mkdir-api/4326/1 "2022-02-01T22:15:39Z")

</div>

I had some thoughts about the `Dir` API. I was thinking `Dir.mkdir` is a little redundant and `Dir.mkdir_p` is a little strange from an API design. Should _dir_ be in the method if the class is _Dir_? Seems to me something like `Dir.create` would be more idiomatic. Also it seems like _p_ should be and option and not a different method. Something like `Dir.create("test/dir", parent: true)` which is what I think `-p` stands from from the man pages.

As a total side note I would wonder if this functionality should be rolled into `.new`?

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [February 1, 2022, 10:28pm UTC](https://forum.crystal-lang.org/t/dir-mkdir-api/4326/2 "2022-02-01T22:28:43Z")

</div>

Yeah \_p is odd. I could see \_r for “recursive”… :) Also like the other ideas.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [February 2, 2022, 10:50am UTC](https://forum.crystal-lang.org/t/dir-mkdir-api/4326/3 "2022-02-02T10:50:56Z")

</div>

Yeah, the name is a bit unfortunate. But then it also makes sense as a resemblance of a well-known command-line tool.

Even if there are better alternatives, I’m not sure if it makes sense to rename it at this point. It would perhaps be clearer to use `Dir.create`. But it’s questionable if the cost for switching are justified by the provided benefit. The current name isn’t really _bad_ in any sense. If starting from scratch, it might not be the best option. But considering existing code bases, it might be the best option to keep it.

I’m not too happy about `mkdir_p`, though. Again, this resemblance is further moved away because it’s a different method instead of an argument to an existing one. Also I think that parents mode should be the default. That’s what you would usually expect, when you want to create a directory `foo/bar/baz/`, you want that path to be created, regardless of whether `foo/` and `foo/bar` already exist or not.  
So, ideally there should be an optional argument for parents mode and I think it should be enabled by default.

I suppose such an API change would support switching to a different name because then the old methods could be cleanly deprecated instead of silently switching the default behaviour of an existing method.

Related discussions for reference:

- [Move Dir.mkdir\_p to FileUtils.mkdir\_p by j8r · Pull Request #6092 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/pull/6092)
- [FileUtils: refactoring · Issue #5231 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/5231)

---

<div class="post-metadata">

**Author:** ![kees](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kees/32/914_2.png) [@kees](https://forum.crystal-lang.org/u/kees)\
**Post date:** [February 2, 2022, 9:21pm UTC](https://forum.crystal-lang.org/t/dir-mkdir-api/4326/4 "2022-02-02T21:21:15Z")

</div>

I think `_p` is for compatibility with the Linux/UNIX `mkdir -p` command:

```auto
-p, --parents
              no error if existing, make parent directories as needed

```

---

<div class="post-metadata">

**Author:** ![paulocoghi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/paulocoghi/32/1429_2.png) [@paulocoghi](https://forum.crystal-lang.org/u/paulocoghi)\
**Post date:** [February 3, 2022, 7:15pm UTC](https://forum.crystal-lang.org/t/dir-mkdir-api/4326/5 "2022-02-03T19:15:23Z")

</div>

~~@kees Interesting to know! Although it still doesn’t need to be exposed as a different function but as an optional parameter, as @straight-shoota mentioned before.~~

Sorry, now I see the context of your post.
