Method: FileUtils.makedirs

Defined in:
lib/fileutils.rb

.makedirsObject

Creates directories at the paths in the given list (a single path or an array of paths), also creating ancestor directories as needed; returns list if it is an array, [list] otherwise.

Argument list or its elements should be interpretable as paths.

With no keyword arguments, creates a directory at each path in list, along with any needed ancestor directories, by calling: Dir.mkdir(path, mode); see Dir.mkdir:

FileUtils.mkdir_p(%w[tmp0/tmp1 tmp2/tmp3]) # => ["tmp0/tmp1", "tmp2/tmp3"]
FileUtils.mkdir_p('tmp4/tmp5')             # => ["tmp4/tmp5"]

Keyword arguments:

  • mode: mode - also calls File.chmod(mode, path); see File.chmod.

  • noop: true - does not create directories.

  • verbose: true - prints an equivalent command:

    FileUtils.mkdir_p(%w[tmp0 tmp1], verbose: true)
    FileUtils.mkdir_p(%w[tmp2 tmp3], mode: 0700, verbose: true)
    

    Output:

    mkdir -p tmp0 tmp1
    mkdir -p -m 700 tmp2 tmp3
    

Raises an exception if for any reason a directory cannot be created.

FileUtils.mkpath and FileUtils.makedirs are aliases for FileUtils.mkdir_p.

Related: FileUtils.mkdir.


393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/fileutils.rb', line 393

def mkdir_p(list, mode: nil, noop: nil, verbose: nil)
  list = fu_list(list)
  fu_output_message "mkdir -p #{mode ? ('-m %03o ' % mode) : ''}#{list.join ' '}" if verbose
  return *list if noop

  list.each do |item|
    path = remove_trailing_slash(item)

    stack = []
    until File.directory?(path) || File.dirname(path) == path
      stack.push path
      path = File.dirname(path)
    end
    stack.reverse_each do |dir|
      begin
        fu_mkdir dir, mode
      rescue SystemCallError
        raise unless File.directory?(dir)
      end
    end
  end

  return *list
end