Need help writing a simple script to chown files

Gavin McCullagh gmccullagh at gmail.com
Thu Aug 2 22:25:26 BST 2007


On Thu, 02 Aug 2007, john  wrote:

> Would you mind explaining this a bit for me? I don't know why the
> variable $i takes the name of the directory, i.e why didn't $i have
> some other value, like my username or the size of the directory or
> whatever? Does "i" have special meaning to the shell?

> > for i in *
> > do
> > chown $i $i
> > done

The bash shell evaulates the * as the files and directories in the current
working directory.  So if you do 
	echo *
you'll get back a plain list of files and directories.  This is much like
running "ls" in the directory, but in one case /bin/ls answers and in the
other bash answers (echo is actually a bash built-in).

So, in the above, we loop over each file or directory, with $i getting the
name.  If each time you do 
	chroot $i $i

Then you're setting ownership of every file to the user of the same name
(assuming that user exists).

For more details on how these bash and other shells work, you can have a
look at this
	http://www.linux.ie/newusers/beginners-linux-guide/shells.php
it's not perfect, but it's mostly correct now that I look back at it.

Gavin




More information about the edubuntu-users mailing list