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

macosxforme macosxforme at gmail.com
Sun Aug 14 08:02:06 PDT 2005


Thanks to you both   !  :)

I'd never seen ls -1  before, thank-you for that one (pun intended).

In this case, I think I won't hit the limit, but that's great to know, an
easy way to split it up if needed.

I went with this:

#!/bin/sh

for i in `ls`; do
if [ -d $i ]
 then
    chown admin:$i $i
    echo "changing owner of directory $i to $I"
 else
    echo "$i is not a directory"
fi 
done


"Cloyce D. Spradling" <cloyce+xunix at headgear.org> wrote:

> On Sun, Aug 14, 2005 at 08:17:08AM -0400, macosxforme wrote:
> : I have a base folder/directory, and within it, will be a long list (800 or
> : so) of directories.
> 
> If it's only 800 or so, try this (Bourne-ish shell assumed; if you're using
> CSH, I don't remember enough to help you:)...
> 
>   for i in `ls`; do
>     chown admin:$i $i
>   done
> 
> At some point you might run into problems with command line length.  On OS X,
> the limit is pretty high (I think around 32KB, but I'm not sure), so that
> should be good for up to at least several thousand users.  Past that, it's
> easy to just break them up into groups, as in
> 
>   for i in `ls -d [a-m]*`; do
>     chown admin:$i $i
>   done
>   for i in `ls -d [n-z0-9]*`; do
>     chown admin:$i $i
>   done
> 
> HTH
> --
> Cloyce
> _______________________________________________

> Gregory Rupp <ruppgregory at yahoo.com> wrote:
> 
> 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.
> 
> 
>> 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




More information about the X-Unix mailing list