2

I have a file setup.py created with Emacs and I set 744 permissions to it:

$ ls -la
total 8
drwxr-xr-x  3 user  staff   96 10 Dec 20:49 .
drwxr-xr-x  6 user  staff  192 10 Dec 20:47 ..
-rwxr--r--  1 user  staff   39 10 Dec 20:49 setup.py

When I edit the file so that Emacs auto-saves, it creates a linked file that represents a lock, which is also executable:

$ ls -la
total 8
drwxr-xr-x  4 user  staff  128 10 Dec 20:56 .
lrwxr-xr-x  1 user  staff   36 10 Dec 20:56 .#setup.py -> user@computer.local.587
drwxr-xr-x  6 user  staff  192 10 Dec 20:47 ..
-rwxr--r--  1 user  staff   39 10 Dec 20:49 setup.py

I save the file with Emacs, and the executable bit resets to 0:

$ ls -la
total 8
drwxr-xr-x  3 user  staff   96 10 Dec 20:56 .
drwxr-xr-x  6 user  staff  192 10 Dec 20:47 ..
-rw-r--r--  1 user  staff   40 10 Dec 20:56 setup.py

I have the same problem if I edit with -q, which skips initialisation:

$ chmod +x setup.py
$ emacs -q setup.py
2019-02-01 10:26:12.136 Emacs-x86_64-10_10[7566:50149] ...
$ ls -la
total 48
drwxr-xr-x  10 user  staff   320  1 Feb 10:26 .
drwxr-xr-x  15 user  staff   480 31 Jan 14:56 ..
-rwxr-xr-x   1 user  staff  2139  1 Feb 10:26 setup.py

The version is 26.1 (9.0).

How can I prevent Emacs from resetting the executable bit?

  • Do you see the same behavior if you start Emacs from emacs -Q? If not, bisect your init file to find the problem. – Drew Dec 10 '18 at 21:27
  • It may be that emacs version and version are important information for this question. Please add that information. – Tobias Dec 10 '18 at 22:10
  • 2
    Is C-h i g (emacs)Backup Copying a factor here? – phils Dec 11 '18 at 0:15
  • @phils Running (setq backup-by-copying 't) forced backup by copying instead of renaming and solved this issue. Do you want to write an answer? – miguelmorin Feb 1 at 10:36
2

As @phils mentioned in a comment, the reason is the default backup policy (from C-h i g (emacs)Backup Copying):

Backup files can be made by copying the old file or by renaming it.

...

The method of making a backup file may also affect the file’s owner and group. If copying is used, these do not change.

The solution is to change this setting in your init file:

(setq backup-by-copying 't)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.