At Tue, 11 Jun 2002 21:57:37 +1000, Jeff Waugh wrote:
> <quote who="Simon Bryan">
> > I have all my users home directories under /home/popusers/
> > it turns out I may need to add a directory and set permissions on it in each
> > users directory (there are over 900 of them).
> > Is there a simple way to do this? Say the directory needs to be called
> > 'attach' in an existing 'dot' directory.
> 
> cd /home
> for i in *; do
>     mkdir -p $i/dot/attach;
>     chown -R $i:$i $i/dot/attach;
>     chmod -R 0755 $i/dot/attach;
> done

or even:

 cd /home
 for i in *; do
   install -d -m 0755 -o $i -g $i $i/dot/attach
 done

of course, this will be a whole lot faster, since there's no forking
required (and will cope with /home's too big to glob):

 #! /usr/bin/perl -w

 use File::Path qw(mkpath);

 opendir HOME, '/home' or die "Can't open /home: $!\n";

 while (defined(my $user = readdir HOME)) {
   my ($login, $pass, $uid, $gid) = getpwnam($user) or next;
   # other conditions, eg:  next unless $uid >= 1000;
   warn "creating /home/$user/dot/attach\n";
   chown $uid, $gid, mkpath("/home/$user/dot/attach");
 }

 closedir HOME;

oh, did i mention there was more than one way to do it? ;)
this is arguably the more "correct" way, rather than groping around
/home (which (eg) wouldn't work at all on automounted /home):

 #! /usr/bin/perl -w

 use File::Path qw(mkpath);
 use User::pwent;  # easier access to getpwent() fields

 while (my $pw = getpwent) {
   next unless $pw->uid >= 1000 and $pw->uid < 10000;
   my $newdir = $pw->dir . '/dot/attach';
   warn "making $newdir\n";
   chown $pw->uid, $pw->gid, mkpath($newdir);
 }

-- 
 - Gus
-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug

Reply via email to