Operating System - HP-UX
1833356 Members
3279 Online
110051 Solutions
New Discussion

Re: Scripting - Changing ownership of files

 
SOLVED
Go to solution
Robin King_1
Regular Advisor

Scripting - Changing ownership of files

Oh scripting gurus...

I have a large number of users (>100) on one of my systems that produce a lot of mail, that they have no need for, and don't housekeep. I've therefore created a .forward file for each of these users, to redirect the mail to /dev/null. I've just realised though, I think the .forward file must be owned by the user, in order for it to work.

Could someone suggest a way of chowning the files to match the owner of the directory in which they reside?

Thanks
9 REPLIES 9
Geoff Wild
Honored Contributor

Re: Scripting - Changing ownership of files

for i in `ls -ld /home/* |awk '{print $3}'`
do
chown $i /home/$i/.forward
done


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Franky_1
Respected Contributor

Re: Scripting - Changing ownership of files

Hi Robin,

i could think of the following :

for i in `ls /home`
do
echo $i
cd $i
chown $i .forward
cd ..
done

Regards

Franky
Don't worry be happy
Muthukumar_5
Honored Contributor

Re: Scripting - Changing ownership of files

We can do as,

# new owner
newowner="new"
for file in `find / -name ".forward"`
do

if [[ $(ls -l $file | awk '{ print $3 }') = "owner" ]]
then

chown $newowner $file

fi

done



HTH.
Easy to suggest when don't know about the problem!

Re: Scripting - Changing ownership of files

(option to ls below is the number 1 not the letter "l", the other option id "d")

ls -1d /home/* | awk -F/ '{print "chown $NF " $0 "/.forward"}' > /tmp/chg_own.sh

(Eyeball the list for correctness)

Then:
sh /tmp/chg_own.sh

This way you will have a record of what you did.
Muthukumar_5
Honored Contributor

Re: Scripting - Changing ownership of files

We can do this one line as,

find / -name ".forward" -exec ll {} \; | awk '{ if ( $3 == "user" ) print "chown " $9 }' | sh

Easy to suggest when don't know about the problem!
Sundar_7
Honored Contributor
Solution

Re: Scripting - Changing ownership of files

Not going by the assumption that user's home directory is in /home, I would try something like this

logins | awk '{print $1}' | xargs -n1 | while read USER
do
GRP=$(groups $USER | awk '{print $1}')
chown ${USER}:${GRP} ~${USER}/.forward
done

Sounds good ?
Learn What to do ,How to do and more importantly When to do ?
Robin King_1
Regular Advisor

Re: Scripting - Changing ownership of files

Spot on Sundar, that worked perfectly for what I was looking to do, as I have home directories all over the place.
Peter Nikitka
Honored Contributor

Re: Scripting - Changing ownership of files

Please keep in mind, that Sundars 'global' solution requires additional priveleges, if Home directories are NFS-mounted (root-access).

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sundar_7
Honored Contributor

Re: Scripting - Changing ownership of files

Peter - It is implied that Robin has enough privileges since he was able to create .forward file in the first place.
Learn What to do ,How to do and more importantly When to do ?