Script for mkdir with permissions


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script for mkdir with permissions
# 1  
Old 08-10-2014
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:
Code:
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.

Last edited by Don Cragun; 08-10-2014 at 01:09 AM.. Reason: Please use CODE and ICODE tags.
# 2  
Old 08-10-2014
Did you consider umask? And, do you know the -p option to mkdir?
# 3  
Old 08-10-2014
Quote:
Originally Posted by jimothy007
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:
Code:
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.
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:
Code:
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).

Last edited by Don Cragun; 08-10-2014 at 07:31 AM.. Reason: Corrrect subdirectory pathnames.
# 4  
Old 08-10-2014
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?
# 5  
Old 08-10-2014
What OS are you using?
Can you use a 1993 or later version of ksh instead of bash?
# 6  
Old 08-10-2014
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.
# 7  
Old 08-10-2014
With a recent version of ksh93, you could just use something like:
Code:
mkdir -m 0770 /userdata}/folder{1..20} /userdata}/folder{2..20..2}/subdir

Since you want to use bash, you can try something like:
Code:
list=""
mkdir -m 0770 /userdatar/folder{1..20}
for i in /userdata/folder*[02468]
do	list="$list $i/subdir"
done
mkdir -m 0770 $list

This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cd then mkdir from script

Importing images from a camera (or two). I sort by date (1901 this month). Currently (failing) if ] then echo "Found Panasonic G9X" #echo "List files on camera" #ls ${pana}/* . chxdir.sh ${photos}/$mn I want to change directory to a fixed base ($photos)/$mn... (10 Replies)
Discussion started by: dpawson
10 Replies

2. Shell Programming and Scripting

Use script to mkdir based on file's data

I have a file with lines like: 111 12 7 111 13 8 112 12 9 115 31 3 120 31 9 123 10 7 125 12 I want to make a script which, split the first column into parts (101-110, 111-120...), and make directories for its part with name (101-110, 111-120...) Also i want in every directory include... (7 Replies)
Discussion started by: efsarantis
7 Replies

3. Shell Programming and Scripting

Bash script: "mkdir -p" doesn't work with var(cat x)

Hello, :) I've an issue with the creation of a directory, All work without it :mad: So, below, my scripts with the debug output : #!/bin/bash # PATHS HOME_BACKUP="/home/backup" HOME_SCRIPT="/home/scripts/test/backup_server" TARGET="/var/www" # DATE DATE_Ymd=$(date +%Y-%m-%d) #... (1 Reply)
Discussion started by: Arnaudh78
1 Replies

4. Shell Programming and Scripting

Help on script to change permissions

Hi I have written the following script that later I want to put in cron,: #!/bin/bash _find="/usr/bin/find" _paths="/moneta_polled01/mediation_gsm /moneta_polled01/mediation_mmsc" for d in $_paths do $_find $d -type f -exec chmod 777 {} \; done but it does not seem to be... (8 Replies)
Discussion started by: fretagi
8 Replies

5. Shell Programming and Scripting

Script Permissions

I have a script were I issue the following command: perl -p -i -e "s/$v31a/$db2/" $dbn31 My script fails with permission error. However, at the command line I can edit the directory in question. I am using uname 002 in my script. So, why can i update manually but not in a script? (10 Replies)
Discussion started by: Harleyrci
10 Replies

6. Shell Programming and Scripting

Permissions script

Hi there, I've been having some frustrating problems with a permissions script. Basically I want it to to determine what permissions the owner, group and everybody has for the file passed in. Here's what I've got: #!bin/bash echo -n "Enter filename: " read file owner=$( ls -l $file | cut... (7 Replies)
Discussion started by: user_invalid
7 Replies

7. Shell Programming and Scripting

mkdir: copying a folder's permissions

Hi, I wanted to know how to create a folder using mkdir and then have it copy the permissions from another specified folder on the system. For example if I did this: mkdir /Volumes/USBSTICK/System How can I make it copy the permissions from the /System folder ? Thanks (4 Replies)
Discussion started by: pcwiz
4 Replies

8. Shell Programming and Scripting

find jpg's mkdir script help

I am having a problem getting this to work right. The script needs to search through directories and subdirectories. If a jpg is found then create a folder in that directory, so on and so forth. Here is what I have so far but it doesn't work right. Help please #!/bin/bash for d in `find ./... (1 Reply)
Discussion started by: jedhypes
1 Replies

9. Shell Programming and Scripting

Modify Perl script to work with txt - Permissions script

Hi I have this code, and i want work with a ls -shalR output in .txt What i need read to do this?? Where start? #!/usr/bin/perl # Allrights- A perl tool for making backups of file permissions # Copyright (C) 2005 Norbert Klein <norbert@acodedb.com> # This program is free... (1 Reply)
Discussion started by: joangopan
1 Replies

10. Shell Programming and Scripting

Shell script permissions

Hi, I'm trying to execute a shell script from application (Informatica). The script works fine when I execute it from command line but fails when executed from Informatica. The tricky part is that there is other scripts in the same folder that can be executed succesfully from both command line... (1 Reply)
Discussion started by: tmikahan
1 Replies
Login or Register to Ask a Question