Script for mkdir with permissions

Hello,

I'm pretty new to scripting and trying to do a simple (well, I thought so) administrator task. I'm using bash .

I want to create 10 directories under the one directory and apply permissions at the same time.

I've worked out the make directories part:

mkdir  /userdata/folder{1..50}

Can I apply permissions to each directory at the same time as creation? And can I also add more arguments such as creating another directory under only specific directories?

Thanks.

Did you consider umask ? And, do you know the -p option to mkdir ?

If you want to create multiple levels of directories and you want to exactly specify the mode of all the directories being created, don't use the -p option to create subdirectories; instead use the -m mode option where the mode option-argument specifies the mode you want all of the newly created directories to have and specify all of the directories that need to be created as operands. The mode option-argument can be in any of the formats used when specifying a mode for the chmod utility. (Just be sure to give the paths of parent directories as operands before any subdirectories that you want to create in those parent directories. And, note that if you specify a mode that doesn't include write mode for the owner class, attempts to create subdirectories will fail.)

Note that you said you wanted to create 10 directories, but:

mkdir -m 0770 /userdata/folder{1..50} /userdata/folder1/subdir /userdata/folder5/subdir_xyz

will create 52 directories; not 12 (with permissions on all of them set to allow read, write, and search by the user and group classes with no access by the other class).

Thanks Don,

Is there a way to also automate the creation of subdirectories depending on a condition?

So doing all of the above and also autmatically create a subdirectory for every directory ending in an even number?

I'm thinking a script is needed?

What OS are you using?
Can you use a 1993 or later version of ksh instead of bash ?

I'm using CentOS but want to stick to bash as I'm still learning and want to stick to the same shell for now. I'm reading up on scripts and I'm thinking using a if, else type script - just not sure how to make it work exactly.

With a recent version of ksh93 , you could just use something like:

mkdir -m 0770 /userdata}/folder{1..20} /userdata}/folder{2..20..2}/subdir

Since you want to use bash , you can try something like:

list=""
mkdir -m 0770 /userdatar/folder{1..20}
for i in /userdata/folder*[02468]
do	list="$list $i/subdir"
done
mkdir -m 0770 $list

Thanks Don, I'll give it a go.