File modes are the filesystem permission bits for who can read, write or execute a file.

In DiredMode you can set these with <code>M</code> (<code>dired-do-chmod</code>).  TarMode and ArchiveMode have the same <code>M</code> key, in those cases operating on archive members.

In Lisp there are functions <code>file-modes</code> to get the modes and <code>set-file-modes</code> to set them.

== chmod ==

As of Emacs 23.1, <code>set-file-modes</code> is also an interactive function that accepts both octal permissions (for old-school Unix users) and symbolic notation similar to the <code>chmod</code> command from GNU coreutils (eg <code>a+r</code>). It can also be called via `M-x chmod', thanks to an alias.

=== chmod on Emacs 22.3 and earlier ===

Here is a <code>chmod</code> command that works on older Emacs (it accepts only octal)

<pre>
(defun chmod (mode)
  "Set the mode of the current file, in octal, as per the chmod program.
If the current file doesn't exist yet the buffer is saved to create it."
  (interactive "sMode (3 or 4 octal digits): ")
  (or (string-match "[0-3]?[0-7][0-7][0-7]" mode)
      (error "Invalid mode"))
  ;; make sure the file exists
  (unless (file-exists-p (buffer-file-name))
    (save-buffer))
  (set-file-modes (buffer-file-name) (string-to-number mode 8)))
</pre>

Lisp:misc-cmds.el has a command that simply calls the <code>chmod</code> program (so it can support symbolic notation).

== Executables ==

You can set the execute bit on a script with <code>M-x executable-set-magic</code>.  It adds or sets the <code>#!</code> interpreter line at the start and sets up to make the file executable when you save it.  You can customize which bits are set using the <code>executable-chmod</code> variable.  The default is 73 (0111 octal) adding executablility for everyone, you could change it to 64 (0100 octal) to do that only for yourself.

ShellMode has <code>C-c :</code> (<code>sh-set-shell</code>) as a shortcut, prompting just for one of common shells (<code>sh</code>, <code>csh</code>, <code>bash</code>, etc).

See also MakingScriptsExecutableOnSave.

== Lisp code ==

To make your file <code>filename</code> readable by just you and your group, use:

    (set-file-modes filename #o440)    ; equivalent to Linux permission "0440"

The assigned mode, in this case octal <code>440</code>, must be an integer. The lisp functions <code>file-modes</code> and <code>file-attributes</code> can be used to recover the current mode settings. Be aware that Windows filesystems offer less resolved access control, so the example presented may not work as intended in a Windows environment.

----
CategoryFiles
