3

Is there a way to make a file read-only from dired?

This, this, and this are unclear to me.

The manual has lots to say about marking/flagging via dired as well as operating on files, but I can't see anything in operating that discusses changing a file to read-only.

Based on the links above (that I said were unclear to me), I am guessing it has something to do with modes and/or permissions?

For bonus points, is there a way to operate on a directory via dired to make all files within it read-only?

To be clear, I want the files to become read-only and to stay that way until they are changed back to read/write manually.

Carl Roberts
  • 433
  • 2
  • 12
  • 4
    How about `C` -- `dired-do-chmod` It works on multiple marked files out of the box. The letter `m` marks files. You can select a region and mark everything in one fell swoop. The doc-string describes additional methods to mark -- `M-x describe-function RET dired-mark RET` "Mark the file at point in the Dired buffer. If the region is active, mark all files in the region. Otherwise, with a prefix arg, mark files on the next ARG lines. If on a subdir headerline, mark all its files except ‘.’ and ‘..’. Use U to remove all marks and u on a subdir to remove the marks in this subdir." – lawlist Nov 07 '16 at 16:52
  • @lawlist: Please post that as an answer. The question is reasonable, and with an explicit answer it can help others. (I suggest leaving out the part about unmarking, and just mentioning that `M` acts on the marked files. The question already points to the doc about marking. Marking and unmarking are themselves really a different question.) – Drew Nov 07 '16 at 17:47
  • @lawlist I understand how to mark files. I do not understand how to make marked files read-only. – Carl Roberts Nov 07 '16 at 17:58
  • Is your question about what **chmod** 3-digit setting makes files read-only, or are you interested in a particular mode available only in Emacs called `read-only-mode`? The function `dired-do-chmod` opens up a minibuffer and you just enter in the 3-digit number of the permissions you wish to set. – lawlist Nov 07 '16 at 18:05
  • @Drew using `M` on a marked file brings up `Change of mode of [file name] to:` What am I to do from there to make the file read-only? – Carl Roberts Nov 07 '16 at 18:06
  • @lawlist When I implement `dired-do-chmod` I get what I said in the above comment to @Drew: `change of mode of [file name] to:` I am not seeing a minibuffer. – Carl Roberts Nov 07 '16 at 18:09
  • What does `C-h k M` tell you? See also the Emacs manual, node [Operating on Files](http://www.gnu.org/software/emacs/manual/html_node/emacs/Operating-on-Files.html). You might need to google for `chmod` (or use `M-x man RET chmod RET`), if you are not familiar with it. Anyway, what you want to enter is something like `u-w` or `a-w`, meaning not writable for the user and not writable for anyone, respectively. – Drew Nov 07 '16 at 18:42
  • @Drew `C-h k M` tells me the key is the command `dired-do-chmod`. Googling `chmod` brings up [this](http://stackoverflow.com/questions/180910/how-do-i-change-read-write-mode-for-a-file-using-emacs), which seems like it is the answer, but it is not. `C-x C-q` does allow me to make the buffer read-only, but I am unable to make the file read-only so that it is still read-only when I re-open the file later, even if I save the buffer when it has been made read-only with `C-x C-q` – Carl Roberts Nov 07 '16 at 19:14
  • 1
    Also, could the problem be that I am on Windows? Is chmod available when using Emacs on windows? Because [this](http://unix.stackexchange.com/questions/47724/how-to-modify-write-permission-on-current-buffer-in-emacs) seems like it would be the answer, but I am getting `'chmod' is not recognized as an internal...` error when I try it. – Carl Roberts Nov 07 '16 at 19:28
  • Are you sure `M` (`dired-do-chmod`) doesn't work on Windows? I just tried in Emacs 24.5 on Windows and it did work for me: answering `-w` at the prompt made the file read-only and answering `+w` made it writeable again. – Omar Nov 07 '16 at 20:21
  • 1
    By the way, `dired-do-chmod` doesn't run the `chmod` program (which would be a problem on Windows, that doesn't have one), but instead uses the Emacs function `set-file-modes` which tries to do the right thing on each OS (cross platform functions like this are one of the things I love the most about using Emacs on Windows: it makes me feel at home, where by "home" I mean Linux!). – Omar Nov 07 '16 at 20:25

1 Answers1

2

As @lawlist said in a comment, use the dired-do-chmod command (which is bound to M). This asks you what mode you want to set on the file, and it expects either a three or four-digit octal number or a symbolic representation of the mode. This is exactly the same value you would give to the chmod program if you were running it at the command line. The difference is that dired-do-chmod works in windows, even though permissions in windows work a different way.

Using an octal number to represent permissions is concise and traditional, but hard to remember and explain. It's basically a bitmask of the permissions, and 600 or 660 are the usual values that you would use. Changing that to 400 or 440 will unset the bits that allow writing, thus making the file readonly. I won't explain further here, but you can look it up if you're curious.

A better way to do it is to to use the symbolic representation. If you use a-w, then you are saying that for all users you want to remove the write permission. Again I won't explain all the possible values you can use here.

This is slightly overkill for just making the file readonly, so you might think about recording a keyboard macro and saving it for future use.

db48x
  • 15,741
  • 1
  • 19
  • 23