Function: dired-do-chmod

dired-do-chmod is an autoloaded, interactive and byte-compiled function defined in dired-aux.el.gz.

Signature

(dired-do-chmod &optional ARG)

Documentation

Change the mode of the marked (or next ARG) files.

Both octal numeric modes like 644 and symbolic modes like g+w are supported. After invoking the command, type M-n (next-history-element) to pull the file attributes of the file at point into the minibuffer.

See Info node (coreutils)File permissions for more information. Alternatively, see the man page for "chmod(1)".

Note that on MS-Windows only the w (write) bit is meaningful: resetting it makes the file read-only. Changing any other bit has no effect on MS-Windows.

View in manual

Probably introduced at or before Emacs version 24.3.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/dired-aux.el.gz
;;;###autoload
(defun dired-do-chmod (&optional arg)
  "Change the mode of the marked (or next ARG) files.
Both octal numeric modes like `644' and symbolic modes like `g+w'
are supported.
After invoking the command, \
type \\<minibuffer-local-completion-map>\\[next-history-element] \
to pull the file attributes
of the file at point into the minibuffer.

See Info node `(coreutils)File permissions' for more information.
Alternatively, see the man page for \"chmod(1)\".

Note that on MS-Windows only the `w' (write) bit is meaningful:
resetting it makes the file read-only.  Changing any other bit
has no effect on MS-Windows."
  (interactive "P" dired-mode)
  (let* ((files (dired-get-marked-files t arg nil nil t))
	 ;; The source of default file attributes is the file at point.
	 (default-file (dired-get-filename t t))
	 (modestr (when default-file
		    (file-attribute-modes (file-attributes default-file))))
	 (default
	   (and (stringp modestr)
		(string-match "^.\\(...\\)\\(...\\)\\(...\\)$" modestr)
		(string-replace
		 "-" ""
		 (format "u=%s,g=%s,o=%s"
			 (match-string 1 modestr)
			 (match-string 2 modestr)
			 (match-string 3 modestr)))))
	 (modes (dired-mark-read-string
		 "Change mode of %s to: "
		 nil 'chmod arg files default))
	 num-modes)
    (cond ((or (equal modes "")
	       ;; Use `eq' instead of `equal'
	       ;; to detect empty input (bug#12399).
	       (eq modes default))
	   ;; We used to treat empty input as DEFAULT, but that is not
	   ;; such a good idea (Bug#9361).
	   (error "No file mode specified"))
	  ((string-match-p "^[0-7]+" modes)
	   (setq num-modes (string-to-number modes 8))))

    (dolist (file files)
      (set-file-modes
       file
       (if num-modes num-modes
	 (file-modes-symbolic-to-number modes (file-modes file 'nofollow)))
       'nofollow))
    (dired-do-redisplay arg))
  (dired-post-do-command))