Need help writing a simple script to chown files

Jim Kronebusch jim at winonacotter.org
Thu Aug 2 18:32:35 BST 2007


> Luckily each directory name is the same as the user who should own the file.
> So what I want to do is have a script that goes recursively through and
> chown's -R the directory called STUDENT_A to be owned STUDENT_A etc.
> 
> So that it looks like:
> 
> drwx------  8 student_a domain users 4096 2007-08-01 15:26 STUDENT_A
> drwx------ 10 ANO_STU_B domain users 4096 2007-08-01 15:26 ANO_STU_B
> 
> I am sure this should be really easy but my attempts to do this have
> failed.
> 
> Anyway, any thoughts would be appreciated!
> 
> Thanks!

I have been running this script for the last 3 years or so now every minute to do close
to what you want:

#!/bin/bash
for file in /home/Drop/*
do owner=$(ls -ld \
"$file"\
|awk '{print $3}')
chown -R $owner \
"$file"
done

I have a folder called Drop in /home/Drop and inside these folders is a folder for every
teacher (/home/Drop/Teachername).  I have set the correct permissions on the teachers
folder but students drop homework into their respective teacher's folders and the newly
written file is owned by the student, when I need it owned by the teacher.  So the
script chowns all files recursively with the owner that is set for the
/home/Drop/teachername.  This isn't exactly what you are looking for but might easily be
modified to what you need.  I haven't tested but I think if you change the "ls -ld" on
line 3 to "ls -l" and change the "print $3" to "print $9" I think it will do what you
want.  See below:

#!/bin/bash
for file in /home/Drop/*
do owner=$(ls -l \
"$file"\
|awk '{print $9}')
chown -R $owner \
"$file"
done

-- 
This message has been scanned for viruses and
dangerous content by the Cotter Technology 
Department, and is believed to be clean.




More information about the edubuntu-users mailing list