Function: make-directory

make-directory is an interactive and byte-compiled function defined in files.el.gz.

Signature

(make-directory DIR &optional PARENTS)

Documentation

Create the directory DIR and optionally any nonexistent parent dirs.

Interactively, the default choice of directory to create is the current buffer's default directory. That is useful when you have visited a file in a nonexistent directory.

Noninteractively, the second (optional) argument PARENTS, if non-nil, says whether to create parent directories that don't exist. Interactively, this happens by default.

Return non-nil if PARENTS is non-nil and DIR already exists as a directory, and nil if DIR did not already exist but was created. Signal an error if unsuccessful.

Other relevant functions are documented in the file group.

View in manual

Probably introduced at or before Emacs version 23.1.

Key Bindings

Shortdoc

;; file
(make-directory "/tmp/bar/zot/" t)

Aliases

mkdir

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun make-directory (dir &optional parents)
  "Create the directory DIR and optionally any nonexistent parent dirs.

Interactively, the default choice of directory to create is the
current buffer's default directory.  That is useful when you have
visited a file in a nonexistent directory.

Noninteractively, the second (optional) argument PARENTS, if
non-nil, says whether to create parent directories that don't
exist.  Interactively, this happens by default.

Return non-nil if PARENTS is non-nil and DIR already exists as a
directory, and nil if DIR did not already exist but was created.
Signal an error if unsuccessful."
  (interactive
   (list (read-file-name "Make directory: " default-directory default-directory
			 nil nil)
	 t))
  ;; If default-directory is a remote directory,
  ;; make sure we find its make-directory handler.
  (setq dir (expand-file-name dir))
  (let ((mkdir (if-let ((handler (find-file-name-handler dir 'make-directory)))
		   #'(lambda (dir)
		       ;; Use 'ignore' since the handler might be designed for
		       ;; Emacs 28-, so it might return an (undocumented)
		       ;; non-nil value, whereas the Emacs 29+ convention is
		       ;; to return nil here.
		       (ignore (funcall handler 'make-directory dir)))
                 #'make-directory-internal)))
    (if (not parents)
        (funcall mkdir dir)
      (let ((dir (directory-file-name (expand-file-name dir)))
            already-dir create-list parent)
        (while (progn
                 (setq parent (directory-file-name
                               (file-name-directory dir)))
                 (condition-case ()
                     (ignore (setq already-dir
                                   (files--ensure-directory mkdir dir)))
                   (error
                    ;; Do not loop if root does not exist (Bug#2309).
                    (not (string= dir parent)))))
          (setq create-list (cons dir create-list)
                dir parent))
        (dolist (dir create-list)
          (setq already-dir (files--ensure-directory mkdir dir)))
        already-dir))))