Operating System - HP-UX
1833199 Members
2991 Online
110051 Solutions
New Discussion

Script for file permission & owner

 
SOLVED
Go to solution
Eric Jacklin
Regular Advisor

Script for file permission & owner

Hi,

i would required .sh script to achive below mention task.

1) chown "defind by me" + chmod "define by me" to all the files with in the folder.

2)it will log all the above activity to a file so that i can check the log and ensure.
8 REPLIES 8
F Verschuren
Esteemed Contributor
Solution

Re: Script for file permission & owner

creat a file that contains all files named list

script:
cat list|while read line
do
chmod ??? $line
chown ????:???? $line
ll $line
done

ofcause you need to fill in the ???

Kind regarts,
Freek
Steven E. Protter
Exalted Contributor

Re: Script for file permission & owner

Shalom,

sounds like homework to me.

The task defined in number 1 above doesn't seem to make sense.

export LOGFILE=/tmp/mylog

chown myid:mygroup * >> $LOGFILE
chmod 777 * >> $LOGFILE

There you go, two command and a log of the activity/output.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: Script for file permission & owner

If there aren't too many filesin the directory you can do:

#!/usr/bin/ksh

# $1 for owner, $2 for permission:
LOGFILE=/tmp/mylog

chown $1 * >> $LOGFILE 2>&1
chmod $2 * >> $LOGFILE 2>&1

Note this only logs errors. If you want to log the whole command, you can add echo before each of the above commands:
echo "chown $1 *" >> $LOGFILE
Deepak Kr
Respected Contributor

Re: Script for file permission & owner

Please use following simple script:
## script to update ownership and permissions on all files in a given directory

!#/bin/sh
export DIR=/directory_path
export LOGFILE=/log_fiel_path

chown -R username:groupname DIR
if[$?==0]
{
echo "`date` >> LOGFILE
echo "Current operation is successful" >> LOGFILE
else
echo "`date` >> LOGFILE
echo "Please check $LOGFILE for errors" >> LOGFILE
fi
chmod -R permission(e.g. 755) DIR
if[$?==0]
{
echo "`date` >> LOGFILE
echo "Current operation is successful" >> LOGFILE
else
echo "`date` >> LOGFILE
echo "Please check $LOGFILE for errors" >> LOGFILE
fi
ls -lrt DIR >> LOGFILE

echo "Please check $LOGFILE for verification"
"There is always some scope for improvement"
Eric Jacklin
Regular Advisor

Re: Script for file permission & owner

Hi

I have tried the scipt give above but it gives following errors.

./newtest.sh[8]: if[0==0]: not found.
./newtest.sh[9]: Syntax error at line 16 : `fi' is not expected.

Please help me out to rectify the same.
Dennis Handly
Acclaimed Contributor

Re: Script for file permission & owner

>it gives following errors.
./newtest.sh[8]: if[0==0]: not found.

The proper syntax is:
if [ $? -eq 0 ]; then

(And remove that "{" on the next line.)

You have a very low point assignment rating:
assigned points to 18 of 164
http://forums.itrc.hp.com/service/forums/pageList.do?userId=CA1422193&listType=unassigned&forumId=1

If you don't fix this, you will be struck off and not get any answers. :-)
Eric Jacklin
Regular Advisor

Re: Script for file permission & owner

Points are assigned.

Still gives the following error any help
# ./newtest.sh
./newtest.sh[8]: if[0: not found.
./newtest.sh[15]: Syntax error at line 16 : `fi' is not expected.



________________________________________
## script to update ownership and permissions on all files in a given directory

#!/bin/sh
export DIR=/tmp/abc
export LOGFILE=/tmp/abc/log_file.log

chown -R hpyjipat:sys DIR
if[$? -eq 0];
echo "`date` >> LOGFILE
echo "Current operation is successful" >> LOGFILE
else
echo "`date` >> LOGFILE
echo "Please check $LOGFILE for errors" >> LOGFILE
fi
chmod -R 700 DIR
if[$? -eq 0];
echo "`date` >> LOGFILE
echo "Current operation is successful" >> LOGFILE
else
echo "`date` >> LOGFILE
echo "Please check $LOGFILE for errors" >> LOGFILE
fi
ls -lrt DIR >> LOGFILE

echo "Please check $LOGFILE for verification"
Dennis Handly
Acclaimed Contributor

Re: Script for file permission & owner

>newtest.sh[8]: if[0: not found.
>if[$? -eq 0];

This isn't exactly what I showed. Every stinkin' space is important. You need to cut & paste:
if [ $? -eq 0 ]; then

>Points are assigned.

You still have to assign points to 130+ replies, back to Jan 2007:
http://forums.itrc.hp.com/service/forums/pageList.do?userId=CA1422193&listType=unassigned&forumId=1