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.

If DIR already exists as a directory, signal an error, unless PARENTS is non-nil.

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.

If creating the directory or directories fail, an error will be raised.

Other relevant functions are documented in the file group.

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.
If DIR already exists as a directory, signal an error, unless
PARENTS is non-nil.

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.

If creating the directory or directories fail, an error will be
raised."
  (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 ((handler (find-file-name-handler dir 'make-directory)))
    (if handler
	(funcall handler 'make-directory dir parents)
      (if (not parents)
	  (make-directory-internal dir)
	(let ((dir (directory-file-name (expand-file-name dir)))
	      create-list parent)
	  (while (progn
		   (setq parent (directory-file-name
				 (file-name-directory dir)))
		   (condition-case ()
		       (files--ensure-directory dir)
		     (file-missing
		      ;; 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)
            (files--ensure-directory dir)))))))