Is there a generalized bash function available that mimics chmod in every aspect, except that it also let's me differentiate between files and directories?
I know there's already numerous examples available, like the following, from this answer:
find /path/to/base/dir -type d -exec chmod 755 {} +
chmod 755 $(find /path/to/base/dir -type d)
find /path/to/base/dir -type d -print0 | xargs -0 chmod 755
...but those are all with hard-coded arguments, and tedious to remember and type out.1)
I'd like to generalize it and make it dynamic, so that I will be able to do something like the following, for instance:
# for directories
chmod -d <any valid argument list for chmod> # or
chmodd <any valid argument list for chmod>
# for files
chmod -f <any valid argument list for chmod> # or
chmodf <any valid argument list for chmod>
I've tried to create a somewhat viable solution myself, but as my bash skills are sub-par and I'm not sure how to parse the correct arguments and insert them into the correct places, it's very crude and limited:
function chmodf {
find . -mindepth 1 -type f -print0 | xargs -0 chmod "$@"
}
function chmodd {
find . -mindepth 1 -type d -print0 | xargs -0 chmod "$@"
}
What I'd preferably like, of course, is something like the following (pseudo code):
function chmodd {
paths = extract paths from arguments list
recursive = extract recursive option from arguments list
otherargs = remaining arguments
if( recursive ) {
find <paths> -mindepth 1 -type d -print0 | xargs -0 chmod <otherargs>
}
else {
find <paths> -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 chmod <otherargs>
}
}
Are you aware of the existence of such a function/binary already, or can you help me on my way to achieving this goal?
The main reason I want this, is that I find myself regularly needing to recursively set the setgid bit on directories, but not on files. But, as far as I am aware there's no capital letter g+S option for chmod.
1) I'm aware of the unix adagio "Do one thing and do it well", or something to that extent, but to be honest, I can't fathom how, at least to my knowledge, after nearly half a century of the existence of chmod and seeing numerous requests for this behavior, chmod has not already been amended with this functionality. It seems like such a obvious and appropriate functionality for chmod to have.