Discussion Locked

Script to Create Directories

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello,

I need a script to do the following:

1) Read list of users from text file
2) create directories for each user
3) chmod the created directories
4) test for existance of previously created directories

More info:

-Have a file users.txt which the script needs to take in and parse.
-script needs to test if /directory/username already exists
-if not exist, create /directory/username
-chmod /directory/username
-next (till EOF)

Any resources, ideas, or script pieces would be greatly appreciated.

Thanks,

4 replies

by opatzg9

I'm not sure what chmod is but here goes:
First MD makes a directory, If the directory already exists then it just doesnot need to create it no problem. You say you have a txt file list, great. Is this a one time job or reocurring? One time job use an editor and create them / flag them whatever.
Short programs to handle this are easy to write or do in a batch script
md @listfile.txt
should make the directories as long as the names are useable as directory names.

by dwdino

I developed my own solution as follows, but thanks.

#!/bin/bash
# script to make user home directories
#

#--- Variable Declarations ---#

DATA1=/dir1/users.txt
DATA2=/dir1/user2.txt
DEST=/dir1/home
declare -a userarray
declare -a delarray

#--- M A I N ---#

# Values assigned to delarray will be used to purge directory
# after new directores made. Carefull of keywords as deletions
# are based upon any directory containing the following strings.
delarray=(toptools admin service kchd KCHD)

# Take user list and remove all line breaks. Then move all
# characters to lower case. Output the result to $DATA2
cat "$DATA1" | tr '\n' ' ' | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'> $DATA2

# Read each string from $DATA2 into array
read -a userarray < $DATA2

# Loop to create directory for each user within the userarray.
# Makes the directory, performs chmod, and returns confirmation.
for user1 in "${userarray[@]}"
{
if test ! -e "$DEST/$user1";
then mkdir $DEST/$user1;
chmod 777 $DEST/$user1;
echo "$user1 's home directory created";
fi
}

# Loop to remove directories designated in delarray.
for user2 in "${delarray[@]}"
{
rm -rf $DEST/*$user2*
}

by dwdino

now, I need to run this script from a web page, so that approved personell can launch the script remotely.

Is this possible?

This discussion is closed to new replies.