# Recursive mkdir

**URL:** <https://www.ruby-forum.com/t/recursive-mkdir/183138>\
**Category:** Ruby\
**Created:** [February 8, 2010, 10:11pm UTC](https://www.ruby-forum.com/t/recursive-mkdir/183138 "2010-02-08T22:11:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [February 8, 2010, 10:11pm UTC](https://www.ruby-forum.com/t/recursive-mkdir/183138/1 "2010-02-08T22:11:47Z")

</div>

So, I was surprised to find out that Dir.mkdir will not create all  
folders in a path when more than just the last level does not exist.  
Example: Dir.mkdir(’/f1/f2/f3’) will not create /f3 when /f2 does not  
exist. I expected it to make both /f2 and /f3 to get the job done. I  
expected it because the language I used most previously did do that.

After some googling I’m not finding any elegant solutions.

Obviously I could split the path at / and iterate through each folder  
name in sequence with an Exists? and mkdir follow up if needed.

Is that really the only option? To do this manually?

Just curious.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [February 8, 2010, 10:29pm UTC](https://www.ruby-forum.com/t/recursive-mkdir/183138/2 "2010-02-08T22:29:28Z")

</div>

On Mon, Feb 8, 2010 at 1:11 PM, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> So, I was surprised to find out that Dir.mkdir will not create all  
> folders in a path when more than just the last level does not exist.  
> Example: Dir.mkdir(‘/f1/f2/f3’) will not create /f3 when /f2 does not  
> exist. I expected it to make both /f2 and /f3 to get the job done. I  
> expected it because the language I used most previously did do that.

Dir.mkdir emulates the unix mkdir command, which behaves this way. It  
shouldn’t be surprising.

> After some googling I’m not finding any elegant solutions.

Did you try ri?

> Obviously I could split the path at / and iterate through each folder  
> name in sequence with an Exists? and mkdir follow up if needed.
> 
> Is that really the only option? To do this manually?

No. Look into FileUtils, specifically FileUtils.mkdir\_p

Ben

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [February 8, 2010, 10:31pm UTC](https://www.ruby-forum.com/t/recursive-mkdir/183138/3 "2010-02-08T22:31:13Z")

</div>

On 2010-02-08, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is that really the only option? To do this manually?

%x{mkdir -p “#{dir}”}  
?

-s

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [February 8, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/recursive-mkdir/183138/4 "2010-02-08T22:18:47Z")

</div>

Greg W. wrote:

> So, I was surprised to find out that Dir.mkdir will not create all  
> folders in a path when more than just the last level does not exist.  
> Example: Dir.mkdir(‘/f1/f2/f3’) will not create /f3 when /f2 does not  
> exist. I expected it to make both /f2 and /f3 to get the job done. I  
> expected it because the language I used most previously did do that.
> 
> After some googling I’m not finding any elegant solutions.
> 
> Obviously I could split the path at / and iterate through each folder  
> name in sequence with an Exists? and mkdir follow up if needed.
> 
> Is that really the only option? To do this manually?

ARGH. FileUtils.mkdir\_p()

(never fails to find the answer right after posting)

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [February 8, 2010, 10:45pm UTC](https://www.ruby-forum.com/t/recursive-mkdir/183138/5 "2010-02-08T22:45:52Z")

</div>

On Feb 8, 4:18 pm, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ARGH. FileUtils.mkdir\_p()

Yea, but set $VERBOSE = true and watch all the pretty warnings.  
Annoying.

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [February 9, 2010, 2:41pm UTC](https://www.ruby-forum.com/t/recursive-mkdir/183138/6 "2010-02-09T14:41:10Z")

</div>

On Feb 8, 10:11 pm, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So, I was surprised to find out that Dir.mkdir will not create all  
> folders in a path when more than just the last level does not exist.  
> Example: Dir.mkdir(‘/f1/f2/f3’) will not create /f3 when /f2 does not  
> exist. I expected it to make both /f2 and /f3 to get the job done. I  
> expected it because the language I used most previously did do that.
> 
> After some googling I’m not finding any elegant solutions.

require ‘fileutils’

FileUtils.mkdir\_p ‘my/path/to/something’
