Unix recursive permission change on files ONLY

NivekC4

Golden Member
Apr 26, 2000
1,800
0
0
In Unix, I was wondering if there was a way to change all file permissions under a certain directory to a certain permission (444) while leaving the directories' permissions as they are (2775).

The chmod -R 444 /<directory>/<directory>/* will change BOTH directories and files. I just want to change the files.

thanks for the help.
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
For tricky chmod stuff like that I don't realy have a proper answer.

I'll also found that using the u+x type things resolve quite a few issues over complicated stuff, but still the recursive directory stuff sucks.

Although there are some ugly scripts I can whip up. (I am a bad and lazy programmer with little knowledge or skills, btw.)


This script is named "chmod.not.dir.bash"
#! /usr/bin/env bash

DIRPATH=$2
PERMIS=$1

find $DIRPATH | while read i
do
if file "$i" |grep -v directory > /dev/null
then
chmod $PERMIS "$i"
fi
done

Then you would go:
chmod.not.dir.bash 444 /were/you/want/to/go

To test to make sure that it'll do what you want:
#! /usr/bin/env bash

DIRPATH=$2
PERMIS=$1

echo "going to do a recursive chmod on $DIRPATH, with permissions $PERMIS"
find $DIRPATH | while read $i
do
if file "$i" |grep directory > /dev/null
then
echo "$i is a directory and will be ignored"
fi
done

Notice that in the first one used 'grep -v' and the second one just used 'grep'

I think that will work, test it out first before using it on a thing with a few thousand files...
 

xcript

Diamond Member
Apr 3, 2003
8,258
2
81
Originally posted by: BingBongWongFooey
a compromise:

find . -type f | while read i; do chmod 444 "$I"; done

Mine handles whitespace fine. :p

xargs with -0 will only terminate at a null.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
And it should be faster because it'll call chmod via xargs with more than one file at a time, meaning less forks.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: xcript
Originally posted by: BingBongWongFooey
a compromise:

find . -type f | while read i; do chmod 444 "$I"; done

Mine handles whitespace fine. :p

xargs with -0 will only terminate at a null.

find -print0 isn't very standard afaik.
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
Originally posted by: BingBongWongFooey
a compromise:

find . -type f | while read i; do chmod 444 "$I"; done

I like that. Normally I stay away from from "find" switches...

I just use it like a "ls -R" should work.... Whoever thought that to list the ls output the way it is should be drawn and quartered.


/directory/name:
file1 file2
file3 file4
file5 file6

/directory/name/2:
file12 file22
file32 file42
file52 file62

etc etc...

How the hell am I suppose to use that???

Oh well, I suppose I should read "man find" that -type switch seems very usefull.
 

StuckMojo

Golden Member
Oct 28, 1999
1,069
1
76

cd dir_you_want_to_start_at
find . -type f -exec chmod 444 {} \;

geez guys, don't any of you know how to use find? :p