[X-Unix] script: setting directory ownership to the name of that directory ?

Gregory Rupp ruppgregory at yahoo.com
Sun Aug 14 06:38:02 PDT 2005


Hey

Sounds like you need a for loop.  Using BASH try something like this:

Here's a test directory:

imac:/tmp/TEST greg$ ls -al
total 0
drwxr-xr-x   5 greg  wheel  170 Aug 14 07:26 .
drwxrwxrwt   9 root  wheel  306 Aug 14 07:25 ..
drwxr-xr-x   2 greg  wheel   68 Aug 14 07:25 user1
drwxr-xr-x   2 greg  wheel   68 Aug 14 07:25 user2
drwxr-xr-x   2 greg  wheel   68 Aug 14 07:26 user3


Then the for loop:

imac:/tmp/TEST greg$ for DIRECTORY in `ls 
-1`                                                           
 > do
 > chown greg:greg $DIRECTORY
 > done


The result:

imac:/tmp/TEST greg$ ls -al
total 0
drwxr-xr-x   5 greg  wheel  170 Aug 14 07:26 .
drwxrwxrwt   9 root  wheel  306 Aug 14 07:25 ..
drwxr-xr-x   2 greg  greg    68 Aug 14 07:25 user1
drwxr-xr-x   2 greg  greg    68 Aug 14 07:25 user2
drwxr-xr-x   2 greg  greg    68 Aug 14 07:26 user3


The "ls -1" gives you the names without all the other stuff so you don't 
need the awk statement.  Change/add statements between the "do" and the 
"done" as needed.

HTH,
Greg


macosxforme wrote:

>I am certain that this is easy for most of you, and probably rather basic...
>
>I have a base folder/directory, and within it, will be a long list (800 or
>so) of directories.
>
>Each directory will bear the name of a user. So, the user will already exist
>on the system (OS X Server, not client)
>
>What I need to do, is read the listing of those folders, and set the owner
>of each directory to be the name of the directory itself - ie:
>chown admin-name:$dirname $dirname   ... ?
>
>I've been fumbling with this for a couple days...
>
>I can do
>ls -l | awk '{print $9}'
>
>(or, could use: basename )
>but how do I store that value, and then use the stored value for chown...  ?
>
>grumble... 
>
>getting the name of each directory and for each directory setting owner (ie:
>chown admin:directoryname directoryname
>and then doing the same on the next directory, etc.
>
>such that
>
>drw-rw-r--    2 admin  usera     68 Aug 13 22:24 usera
>drw-rw-r--    2 admin  userb     68 Aug 13 18:04 userb
>
>
>Any and all input welcome and appreciated, TIA
>
>
>_______________________________________________
>X-Unix mailing list
>X-Unix at listserver.themacintoshguy.com
>http://listserver.themacintoshguy.com/mailman/listinfo/x-unix
>
>Listmom is trying to clean out his closets! Vintage Mac and random stuff:
>         http://search.ebay.com/_W0QQsassZmacguy1984
>
>  
>



More information about the X-Unix mailing list