Computing.Net > Forums > Linux > Create Directories Script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Create Directories Script

Reply to Message Icon

Name: dwp
Date: December 13, 2002 at 08:01:20 Pacific
OS: RH 8
CPU/Ram: 1/1
Comment:

I have been trying to find a script to do the following:

1) open text file containing user list
2) test to see if directory already exists (/directory/username)
3) if not exist, create directory (/directory/username)
4) chmod directory
5) do until EOF

Any ideas, resources, pieces would be greatly appreciated.

Thanks,



Sponsored Link
Ads by Google

Response Number 1
Name: 3Dave
Date: December 13, 2002 at 08:25:10 Pacific
Reply:

Can be quite easily done in perl (my preferred scripting language when it comes to text files) and very possibly in bash too.

The perl script would probably go along the lines of:

open text file as an array (@names?)
foreach @names {
print `mkdir /home/$_`;
(if the folder is already there it will just fail to create it)
print `chmod $_ /home/$_`;
}

That's about it....it's not a working script but should give you a starting point. More help on a perl script can be supplied if you want.


0

Response Number 2
Name: dwp
Date: December 13, 2002 at 08:51:54 Pacific
Reply:

OK,

I understand the structure and constructs, but have no idea how to code.


0

Response Number 3
Name: dwp
Date: December 13, 2002 at 11:03:12 Pacific
Reply:

Here is what I developed:

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

DATA1=/dir1/users.txt
DATA2=/dir1/user2.txt
DEST=/dir1/home

declare -a userarray
declare -a delarray

delarray=() #array of directories to delete (system accounts, etc.)

cat "$DATA1" | tr '\n' ' ' | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'> $DATA2

read -a userarray < $DATA2

for user1 in "${userarray[@]}"
{
mkdir $DEST/$user1
chmod 777 $DEST/$user1
echo "$user1 's home directory created"
}

for user2 in "${delarray[@]}"
{
rm -rf $DEST/*$user2*
}

Not bad for one hours research and work.


0

Response Number 4
Name: armin
Date: December 14, 2002 at 02:12:01 Pacific
Reply:


uhm, am i missing something or can this be achieved much easier?


#!/bin/bash
LIST=/path/to/list

create() {
mkdir $i && chmod 755 $i
}

for i in `cat $LIST`; do
if [ ! -a $i ]; then
create
echo "created $i"
else
echo "$i exists"
fi
done


0

Response Number 5
Name: unixadm2000
Date: December 14, 2002 at 11:51:00 Pacific
Reply:

May want to use "mkdir -p"


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Create Directories Script

simple script www.computing.net/answers/linux/simple-script/20107.html

Shell Script problem..need help! www.computing.net/answers/linux/shell-script-problemneed-help/22814.html

ksh script -nubie- www.computing.net/answers/linux/ksh-script-nubie/27476.html