making multiple directory's quickly

psyman

Registered
OK, scenario is this:

I have 200 users home folders and I want to create an additional folder inside each of their home folder called windows (to hold their windows domain profile).
I want this folder to inherit the permissions from it's parent folder for obvious reasons.

Any of you command line guru's tell me how to do this.

Thanks

PsyMan
 
You'll need a small script.

Run it as root.

Have it read thru' the passwd file, extract the username, dump that into a command string to execute.

In perl, it would look *something* like:

#!/usr/bin/perl

my $file = "/etc/passwd";

open PWD, $file;

while( <PWD> ){
my $line = $_;
# parse out username here
my $user = substr( $line,0,index($line,":") );
my $path = "/Users/$user/windows";
print "$user --> $path\n";
mkdir $path;
#add some code here to set folder perms based on "/Users/$user"
#I don't know enough Perl to do that, other than shelling out to run
#a program to do it. Build a command string like:
my $cmd = "chgrp $user /Users/$user/windows";
#shell and run command
my $otherCmd = "chown $user /Users/$user/windows";
#shell and run command
}

close PWD;

Another option, other than reading the passwd file is to Glob the /Users directory to get the usernames. Same results, more or less. In order to create subdirectories under /Users you have to be root/admin so you might as well read passwd to ensure you get all known users even if they may not have a home directory.
 
I don't think /etc/passwd will give you very good results, since OS X uses Netinfo for that stuff by default. You can do this with just a few lines of bash scripting:

Code:
#!/bin/sh

cd /Users
for uname in *
do
mkdir $uname/windows
chown $uname $uname/windows
chmod 700 $uname/windows
done

Put that into a script file, and run it as root. It should make the directories, set them to be owned by the respective user, and give access to only the owner.

Of course, this assumes that you want it done for everyone in the /Users directory, so you might want to check for extra windows directories in subdirectories that you don't need them afterward (like in Shared, for instance).
 
There ya go! Even better. (not a fan of perl, but it was a good practice for me since I'm being forced to use the cryptic piece of crap)
 
He he. Yeah, that perl looks correct, other than reading from /etc/passwd. I generally don't bother using perl for filesystem manipulation, though, since it's so much simpler with bash. Actual file I/O, on the other hand... ;-)
 
The passwd file is up to date on my apple... contains the user accounts as well as the various system accounts. But yeah, globbing the directory is the better way, the more I think about it. In theory, you're *only* going to get user accounts and the shared stuff.

I'm a C# & Java programmer working in a Perl shop, so I'm being asked to do more perl stuff. I miss my structure and my exceptions (sigh)
 
With a bit of tinkering, reading and your help, I came up with the following and it worked:

for i in /volumes/student_data/group_data/*
do
u=`echo $i | cut -d/ -f5`
echo "Making Windows Directory for "$u
mkdir /volumes/student_data/group_data/$u/Windows
chown $u /volumes/student_data/group_data/$u/Windows
chmod 700 /volumes/student_data/group_data/$u/Windows
done

I am sure I overdid it a bit and your script was simpler but it's nice to learn a little bit every day

must get a book on shell scripting

Thanks for your input
 
When learning scripting, I'd suggets you pick a shell and standardize on it... Bash and tcsh are the most used, others are certainly available, k, bourne, etc.

Each has some built in scripting capability and in some cases, each can be radically different than others.

After some shell scripting, pick a scripting language for more stuff. Perl and Python are the current heavy lifters. Perl enjoys much wider support and it's older, however Python is just as powerful, more readable when you want to go back to fix things later and if you're not a programmer, Python beats the dog snot out of Perl for time-to-deployment for newer users.

After that, if you find yourself needing more solutions within apps and such, AppleScript and/or realBasic would be your next steps. AppleScript is more widely used for scripting and realBasic is more powerful but less supported across a wider selection of applications.
 
Back
Top