Does Ruby have mkdir -p?
require 'fileutils'
FileUtils.mkdir_p 'cool/beans'
How to create directories recursively in ruby?
Use mkdir_p
:
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'
In Ruby 1.9 FileUtils was removed from the core, so you'll have to require 'fileutils'
.
Ruby FileUtils.mkdir_p is only creating parent directories
Because you use dir = File.dirname("#{Rails.root}/public/graph_templates/aaa/test")
,
then the dir
is "#{Rails.root}/public/graph_templates/aaa"
.
You could just pass the path to FileUtils.mkdir_p
.
def create_temporary_template
dir = "#{Rails.root}/public/graph_templates/aaa/test"
FileUtils.mkdir_p dir
end
Create Directory if it doesn't exist with Ruby
You are probably trying to create nested directories. Assuming foo
does not exist, you will receive no such file or directory
error for:
Dir.mkdir 'foo/bar'
# => Errno::ENOENT: No such file or directory - 'foo/bar'
To create nested directories at once, FileUtils
is needed:
require 'fileutils'
FileUtils.mkdir_p 'foo/bar'
# => ["foo/bar"]
Edit2: you do not have to use FileUtils
, you may do system call (update from @mu is too short comment):
> system 'mkdir', '-p', 'foo/bar' # worse version: system 'mkdir -p "foo/bar"'
=> true
But that seems (at least to me) as worse approach as you are using external 'tool' which may be unavailable on some systems (although I can hardly imagine system without mkdir
, but who knows).
FileUtils.mkdir_p doesn't work
Your code worked for me. Have you tried running it with the verbose flag?
FileUtils.mkdir_p("/Users/naorye/.../thumbnails", :verbose => true)
FileUtils.mkdir_p is analogous to the unix shell command mkdir -p
, which creates the terminal directory and all intermediate directories in the path you specify. As for the synonyms mkpath
and makedirs
, they're likely there for the same reason Enumerable
has both inject
and reduce
.
How do I create directory if none exists using File class in Ruby?
You can use FileUtils to recursively create parent directories, if they are not already present:
require 'fileutils'
dirname = File.dirname(some_path)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end
Edit: Here is a solution using the core libraries only (reimplementing the wheel, not recommended)
dirname = File.dirname(some_path)
tokens = dirname.split(/[\/\\]/) # don't forget the backslash for Windows! And to escape both "\" and "/"
1.upto(tokens.size) do |n|
dir = tokens[0...n]
Dir.mkdir(dir) unless Dir.exist?(dir)
end
Why can't I use mkdir with a Pathname object?
You need to specify the directory you want to create with Pathname and then call mkdir
.
This should work:
p = Pathname.new('/tmp/123adam')
p.mkdir
The argument you can supply are the permissions for the new directory.
How do I create a subdirectory if no one exists when writing to a CSV file?
You can do this:
require "fileutils"
csvfile= 'tmp/folder1/folder2/folder3/foo.csv'
FileUtils::mkdir_p File.dirname csvfile
mkdir_p
is line gnu mkdir -p
which creates the directory structure for you, and won't complain if the directory already exists.
dirname
returns the directory name.
If you want specify permissions when creating the directory do this:
FileUtils::mkdir_p( File.dirname(csvfile) , :mode => 0777)
Where does ~/ point in Ruby?
In Ruby, ~
has no special meaning in file paths. Even if it is used inside the parameter of a system
call, it is not expanded by the underlying shell. Your code should have created a directory literally named ~
inside the current working directory, for example:
$ ruby -e 'system("mkdir", "-p", "~/.dir")'
$ ls
~
$ ls -A '~'
.dir
You have to use File.expand_path
to expand the ~
to your home directory path:
File.expand_path('~')
# => "/home/toro2k"
In your example:
system('mkdir', '-p', File.expand_path('~/.dir'))
In Ruby you can also use FileUtils.mkdir_p
to create directories:
require 'fileutils'
FileUtils.mkdir_p(File.expand_path('~/.dir'))
Update: as suggested by the Tin Man my latter example can be rewritten using the Pathname
class as follows:
require 'pathname'
Pathname.mkpath(Pathname.new('~/.dir').expand_path)
FileUtils.mkdir can not create a directory
you need FileUtils.mkdir_p "/tmp/bar/foo"
mkdir_p
behaves exactly as mkdir -p
on UNIXes - if some dir does not exists it will be created.
I bet there are no /tmp/bar
dir and Ruby fails to create a dir into an non-existing folder.
Related Topics
Respond_To' VS. 'Respond_To_Missing'
How to Load a Spec_Helper.Rb Automatically in Rspec 2
Rails: How to Show User's "Last Seen At" Time
How to Reflect in the Database a New Belongs_To and Has_Many Relationship in Ruby on Rails
How Might I Pass Text Data from the Ruby Console into My Clipboard Without Saving to a File
How to Use Active Record Without Rails
How to Return a Value from a Thread in Ruby
Generate Ruby Classes from Xsd
How to Check from Ruby Whether a Process with a Certain Pid Is Running
Rails Routing (Root :To => ...)
Can't Convert Fixnum to String During Rake Db:Create
What Are the Uppercase and Lowercase Rules of Ruby Method Name