5

Given the following directory tree:

.
├── d1
│   └── workspace
├── d2
│   └── workspace
├── d3
│   └── workspace
├── d4
│   └── workspace
└── d5
    └── workspace

I need to set the permissions for all workspace directories as below:

chmod -R 774 d1/workspace
chmod -R 774 d2/workspace
...

How can I do the above operations in one command for all workspace directories? I can run the following command:

chmod -R 774 *

But this also changes the mode of parent directories, which is not desired.

  • 774? That doesn't make a lot of sense. – Michael Hampton Mar 18 '18 at 14:38
  • 1
    The -4 at the end means that anyone is welcome to read the directory listing, but cannot do anything else with the files in it. For a directory with group write access, 775 (= anyone can read the directory and may access the files if their permissions allow it) or 770 (no access to anyone other than owner and the authorized group) are the more common choices. – telcoM Mar 19 '18 at 9:44
15

You can use wildcards on the top level directory.

chmod 774 d*/workspace

Or to make it more specific you can also limit the wildcard, for example to d followed by a single digit.

chmod 774 d[0-9]/workspace

A more general approach could be with find.

find d* -maxdepth 1 -name workspace -type d -exec chmod 774 "{}" \;
  • 4
    Maybe add -type d to your find command? – user1404316 Mar 18 '18 at 11:26
  • As @user1404316 has pointed out, it'd be better to use the -type d argument with find, so it only returns directories, rather than hoping there's no files which names start with 'd'. – djsmiley2k Mar 18 '18 at 13:47
  • d*/ and d[0-9]/ will match only directories whose names begin with d, and the find command doesn’t look at names beginning with d at all. The issue is avoiding files named workspace. – Scott Mar 18 '18 at 15:02
  • Whether they actually want it or not, the OP did use a -R flag, so perhaps they want the permissions recursively set under the workspace directories. – Jeff Schaller Mar 18 '18 at 23:47
  • @JeffSchaller: you are right. I was reading the question as if the permissions only has to be set on the workspace directory. @Meysam: maybe you can clarify if it was intended to set the permissions on the workspace directory only or do that recursive on those directories. – Thomas Mar 19 '18 at 7:24
4

In a shell like bash you can use its extended its globbing option to first mark all the directories named workspace and chmod it in one shot

shopt -s nullglob globstar

The option nullglob is make sure the glob expansion does not throw any error when it does not find any files in the path. Also this will ensure the empty glob string is not included as part of the array. The globstar option is enabled for recursive globbing.

Now mark those directories in a shell array as

dirs=(**/workspace/)

As one more sanity check, you could first print the array to see if all the directories required are taken care. See if all the directories are listed below when you do the below printf() command,

printf '%s\n' "${dirs[@]}"

This will populate the array with all recursive workspace folders, now we need to use chmod on it

(( "${#dirs[@]}" )) && chmod -R 774 -- "${dirs[@]}"
0

The chmod command has a nice shortcut for setting the executable bit only on directories, like so:

chmod a+X *

This is very handy to make a whole directory tree readable by anyone, but not setting the executable bit on any regular files:

chmod -R a+rX *
  • 2
    X sets the executable bit on directories and any files with an executable bit set. Generally speaking that doesn’t make much difference (since executables usually have the bit set for everyone), but it can be significant in some cases. Also note that you can’t get mode 774 using this trick. – Stephen Kitt Mar 18 '18 at 12:14
-1
find . -mindepth 1 -type d -exec chmod 774 {} \;

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.