How to chmod only on subdirectories?
up vote
5
down vote
favorite
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.
bash permissions chmod recursive
add a comment |
up vote
5
down vote
favorite
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.
bash permissions chmod recursive
774? That doesn't make a lot of sense.
– Michael Hampton
Mar 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 at 9:44
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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.
bash permissions chmod recursive
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.
bash permissions chmod recursive
bash permissions chmod recursive
edited Mar 18 at 7:50
asked Mar 18 at 7:13
Meysam
1,51882240
1,51882240
774? That doesn't make a lot of sense.
– Michael Hampton
Mar 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 at 9:44
add a comment |
774? That doesn't make a lot of sense.
– Michael Hampton
Mar 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 at 9:44
774? That doesn't make a lot of sense.
– Michael Hampton
Mar 18 at 14:38
774? That doesn't make a lot of sense.
– Michael Hampton
Mar 18 at 14:38
1
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 at 9:44
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 at 9:44
add a comment |
4 Answers
4
active
oldest
votes
up vote
15
down vote
accepted
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 yourfind
command?
– user1404316
Mar 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 at 13:47
d*/
andd[0-9]/
will match only directories whose names begin withd
, and thefind
command doesn’t look at names beginning withd
at all. The issue is avoiding files namedworkspace
.
– Scott
Mar 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 at 23:47
@JeffSchaller: you are right. I was reading the question as if the permissions only has to be set on theworkspace
directory. @Meysam: maybe you can clarify if it was intended to set the permissions on theworkspace
directory only or do that recursive on those directories.
– Thomas
Mar 19 at 7:24
add a comment |
up vote
4
down vote
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 '%sn' "${dirs[@]}"
This will populate the array with all recursive workspace
folders, now we need to use chmod
on it
(( "${#dirs[@]}" )) && chmod -R 774 -- "${dirs[@]}"
add a comment |
up vote
0
down vote
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 at 12:14
add a comment |
up vote
-1
down vote
find . -mindepth 1 -type d -exec chmod 774 {} ;
New contributor
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
15
down vote
accepted
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 yourfind
command?
– user1404316
Mar 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 at 13:47
d*/
andd[0-9]/
will match only directories whose names begin withd
, and thefind
command doesn’t look at names beginning withd
at all. The issue is avoiding files namedworkspace
.
– Scott
Mar 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 at 23:47
@JeffSchaller: you are right. I was reading the question as if the permissions only has to be set on theworkspace
directory. @Meysam: maybe you can clarify if it was intended to set the permissions on theworkspace
directory only or do that recursive on those directories.
– Thomas
Mar 19 at 7:24
add a comment |
up vote
15
down vote
accepted
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 yourfind
command?
– user1404316
Mar 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 at 13:47
d*/
andd[0-9]/
will match only directories whose names begin withd
, and thefind
command doesn’t look at names beginning withd
at all. The issue is avoiding files namedworkspace
.
– Scott
Mar 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 at 23:47
@JeffSchaller: you are right. I was reading the question as if the permissions only has to be set on theworkspace
directory. @Meysam: maybe you can clarify if it was intended to set the permissions on theworkspace
directory only or do that recursive on those directories.
– Thomas
Mar 19 at 7:24
add a comment |
up vote
15
down vote
accepted
up vote
15
down vote
accepted
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 "{}" ;
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 "{}" ;
edited Mar 18 at 15:31
peterh
4,08792956
4,08792956
answered Mar 18 at 7:58
Thomas
3,65141225
3,65141225
4
Maybe add-type d
to yourfind
command?
– user1404316
Mar 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 at 13:47
d*/
andd[0-9]/
will match only directories whose names begin withd
, and thefind
command doesn’t look at names beginning withd
at all. The issue is avoiding files namedworkspace
.
– Scott
Mar 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 at 23:47
@JeffSchaller: you are right. I was reading the question as if the permissions only has to be set on theworkspace
directory. @Meysam: maybe you can clarify if it was intended to set the permissions on theworkspace
directory only or do that recursive on those directories.
– Thomas
Mar 19 at 7:24
add a comment |
4
Maybe add-type d
to yourfind
command?
– user1404316
Mar 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 at 13:47
d*/
andd[0-9]/
will match only directories whose names begin withd
, and thefind
command doesn’t look at names beginning withd
at all. The issue is avoiding files namedworkspace
.
– Scott
Mar 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 at 23:47
@JeffSchaller: you are right. I was reading the question as if the permissions only has to be set on theworkspace
directory. @Meysam: maybe you can clarify if it was intended to set the permissions on theworkspace
directory only or do that recursive on those directories.
– Thomas
Mar 19 at 7:24
4
4
Maybe add
-type d
to your find
command?– user1404316
Mar 18 at 11:26
Maybe add
-type d
to your find
command?– user1404316
Mar 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 at 13:47
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 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 at 15:02
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 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 at 23:47
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 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 at 7:24
@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 at 7:24
add a comment |
up vote
4
down vote
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 '%sn' "${dirs[@]}"
This will populate the array with all recursive workspace
folders, now we need to use chmod
on it
(( "${#dirs[@]}" )) && chmod -R 774 -- "${dirs[@]}"
add a comment |
up vote
4
down vote
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 '%sn' "${dirs[@]}"
This will populate the array with all recursive workspace
folders, now we need to use chmod
on it
(( "${#dirs[@]}" )) && chmod -R 774 -- "${dirs[@]}"
add a comment |
up vote
4
down vote
up vote
4
down vote
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 '%sn' "${dirs[@]}"
This will populate the array with all recursive workspace
folders, now we need to use chmod
on it
(( "${#dirs[@]}" )) && chmod -R 774 -- "${dirs[@]}"
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 '%sn' "${dirs[@]}"
This will populate the array with all recursive workspace
folders, now we need to use chmod
on it
(( "${#dirs[@]}" )) && chmod -R 774 -- "${dirs[@]}"
edited Mar 19 at 8:09
answered Mar 18 at 7:52
Inian
3,775824
3,775824
add a comment |
add a comment |
up vote
0
down vote
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 at 12:14
add a comment |
up vote
0
down vote
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 at 12:14
add a comment |
up vote
0
down vote
up vote
0
down vote
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 *
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 *
answered Mar 18 at 10:51
G. Sliepen
1092
1092
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 at 12:14
add a comment |
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 at 12:14
2
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 at 12:14
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 at 12:14
add a comment |
up vote
-1
down vote
find . -mindepth 1 -type d -exec chmod 774 {} ;
New contributor
add a comment |
up vote
-1
down vote
find . -mindepth 1 -type d -exec chmod 774 {} ;
New contributor
add a comment |
up vote
-1
down vote
up vote
-1
down vote
find . -mindepth 1 -type d -exec chmod 774 {} ;
New contributor
find . -mindepth 1 -type d -exec chmod 774 {} ;
New contributor
edited 2 days ago
Jeff Schaller
36.3k952119
36.3k952119
New contributor
answered 2 days ago
Neal Garrett
1
1
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f431881%2fhow-to-chmod-only-on-subdirectories%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
774? That doesn't make a lot of sense.
– Michael Hampton
Mar 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 at 9:44