Operating System - HP-UX
1838374 Members
3101 Online
110125 Solutions
New Discussion

Changing file permissions

 
SOLVED
Go to solution
Kristopher March
Regular Advisor

Changing file permissions

Is there a way to change file permissions with the for do done loop?

If I have files with permissions of 777 and I want to change all to 644, is there a way to change them in one shot.
It seems easy, I just can't think how to do it.

Having a hard time grasping the different loops in shell scripting. Is there a good site to check out with examples. I have books but I need something new to look at.
"This ain't no burger flippin job!"
11 REPLIES 11
Chris Wilshaw
Honored Contributor
Solution

Re: Changing file permissions

If you wish to change them in the current directory, and all subdirectories, use

find . -perm 777 -exec chmod 644 {} \;

Pete Randall
Outstanding Contributor

Re: Changing file permissions

You can just chmod 644 /dir/file*. Or you could use a find command - find /dir -name xxx* -exec chmod 644 {} \;

HTH,
Pete

Pete
Kristopher March
Regular Advisor

Re: Changing file permissions

That's right! Thanks.
"This ain't no burger flippin job!"
Ceesjan van Hattum
Esteemed Contributor

Re: Changing file permissions

Many ways to write such a script. One possiblity is:

for i in `find / -exec ls -a {} \;`
do
chmod 644 $i
done

Note, a 'ls' won't do, because it does not contain a full path.

Regards,
Ceesjan
Steve Steel
Honored Contributor

Re: Changing file permissions

Hi


find . -type f -perm 777|xargs chmod 644


find . -type f -perm 777|while read line
do
chmod 644 $line
done


steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Pete Randall
Outstanding Contributor

Re: Changing file permissions

Or chmod -R

Pete

Pete
Leif Halvarsson_2
Honored Contributor

Re: Changing file permissions

Hi

Perhaps you should use -type to get only files (not directorys).
finf . -type f -perm 777 -exec chmod 777 {}\;
Trond Haugen
Honored Contributor

Re: Changing file permissions

If you task is to change all the files within a certain directory and subdirectories you can do 'chmod -R 644 dir2change'. Of course you chouldn't do that to a dir.
You can do that a little better with find: 'find dir2change -type f -exec chmod 644 {} \;'.
Now if you want to put that in a loop:
for FILE in $(find dir2change -type f -print)
do
chmod 644 $FILE
done

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Leif Halvarsson_2
Honored Contributor

Re: Changing file permissions

sorry
Of cource not chmod 777, but chmod 644.
Jeff Schussele
Honored Contributor

Re: Changing file permissions

Hi Kristopher,

Try using

find . -perm 777 -exec chmod 644 {} \;

OR

find . -perm 777 -print | xargs chmod 644

Either way will do it for you w/o having to use an explicit loop.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Keely Jackson
Trusted Contributor

Re: Changing file permissions

hi

Yes you can. For example change everything in /home/me

for i in `ls /home/me`
do
chmod 644 $i
done

For a web site if you are using the korn shell
try http://www.kornshell.com/

Cheers
Keely

Live long and prosper