Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

FileUtils.mkdir

(::FileUtils::pathlist list, ?mode: ::Integer?, ?noop: ::boolish, ?verbose: ::boolish) -> void
FileUtils.mkdir(*paths, mode: 0775, noop: nil, verbose: nil) → array_of_paths or nil

Argument paths may be one path or an array of paths; each must be interpretable as a path.

By default, creates a directory entry at each of the given paths; each directory has permissions 0775; returns an array of the paths:

Example
FileUtils.mkdir('foo')                 # => ["foo"]
FileUtils.mkdir(%w[bar baz])           # => ["bar", "baz"]
File.stat(Dir.new('foo')).mode.to_s(8) # => "40775"
File.stat(Dir.new('bar')).mode.to_s(8) # => "40775"
File.stat(Dir.new('baz')).mode.to_s(8) # => "40775"
FileUtils.rmdir(%w[foo bar baz])       # => ["foo", "bar", "baz"]

Raises an exception if for any reason a directory cannot be created; a common reason is that the directory already exists.

With keyword argument mode given, creates directories with the given permissions:

Example
FileUtils.mkdir('bar', mode: 0664)
File.stat(Dir.new('bar')).mode.to_s(8) # => "40664"
FileUtils.rmdir('bar')

With keyword argument noop given as true, does not create directories; returns nil

Example
FileUtils.mkdir('foo', noop: true) # => nil
Dir.exist?('foo')                  # => false

With keyword argument verbose given as true, prints a shell command:

Example
FileUtils.mkdir('foo', verbose: true)
FileUtils.mkdir(%w[bar baz], mode: 0644, verbose: true)
FileUtils.mkdir(%w[bar baz], mode: 0644, verbose: true, noop: true)

Output:

mkdir foo
mkdir -m 644 bar baz
mkdir -m 644 bar baz

Note that in the last call above (containing noop: true) the existence of the directory is not checked.

Related: FileUtils.mkdir_p.

See also #mkdir .makedirs .mkdir_p .ln_sr also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0