tags:

views:

36

answers:

3

I want to store a file as /a/b/c/d.txt, but I do not know if any of these directories exist and need to recursively create them if necessary. How can one do this in ruby?

A: 
 require 'ftools'

File.makedirs

ormuriauga
Thanks for the pointer! The docs seem to prefer FileUtils.mkdir_p, so I took that...
Jan Limpens
+2  A: 

Try this:

 FileUtils.mkdir_p '/a/b/c'

The "_p" is a unix holdover for parent/path you can also use the alias mkpath if that makes more sense for you.

FileUtils.mkpath '/a/b/c'

Reference this page here.

Harmon Wood
+1. `FileUtils` and `Pathname` are probably *the* most important tools for Ruby shell scripting.
Jörg W Mittag
A: 

If you are running on unixy machines, don't forget you can always run a shell command under ruby by placing it in backticks.

`mkdir -p /a/b/c`
Matthew Schinckel