2

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.

  • 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 7 '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 7 '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 7 '16 at 17:58
  • 1
    Also, could the problem be that I am on Windows? Is chmod available when using Emacs on windows? Because this 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 7 '16 at 19:28
  • 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 7 '16 at 20:25
1

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.

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.