1

I have a directory with many subdirectories and files in it, and I would like to change the permissions of the files there the following ways. Basically, I want to protect the directories better.

First, all DIRECTORIES should be just chmod to 750 recursively, but only directories.

For files: if a file has a +x for the main user, it should be changed to 750, otherwise it should be changed to 640 (meaning no +x, r/w for the main user, r for the group, no permissions for world).

This should be applied to all files in all directories.

Is there a way to do that?

2 Answers 2

3

The X (capital X) mode character in chmod does the difficult bit: it lets you add execution permission only if it was already present for some user.

chmod -R g+rX-w,o= /path/to/directory

(Add user permission changes if desired.)

0

By "main user," I assume you mean the file owner? Find would probably work for you. Assuming /dir is the directory you're doing all this in:

# find /dir -type d -exec chmod 750 {} \;
# find /dir -type f -perm -u-x -exec chmod 640 {} \;
# find /dir -type f -perm -u+x -exec chmod 750 {} \;

The first one changes all directories to 750. The second one changes files without owner executable permission to 640. The third one changes the files with +x for the user to 750. These could probably be combined into a single find command but that would take a little more time to work out than I have to spend right now.

3
  • that `;\` seems a little odd.
    – thrig
    Commented Feb 14, 2017 at 23:00
  • Your second find should exclude all files that u+x set, otherwise it will change the permissions to 640 for those files, and the third find command won't work.
    – Munir
    Commented Feb 14, 2017 at 23:01
  • oh you're right, I didn't think of that. Editing now. @tthrig, I always get the ordering of those backwards. corrected. (I'm on my Windows work computer and can't plug into a linux box to test these so I'm doing them from memory, I think it's correct now though).
    – Kefka
    Commented Feb 14, 2017 at 23:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .