LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying to traverse a directory to change file permissions (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-traverse-a-directory-to-change-file-permissions-482876/)

bskrakes 09-12-2006 05:07 PM

Trying to traverse a directory to change file permissions
 
Hello all! I don't want to say that I am a complete newbie but I am pretty "fresh of the boat."

Anyway down to the isse.... I am trying to traverse a directory to change the file/permission settings of any/all the files within that directory. Here is what I came up with (no not on my own, mostly from web sites around that show bash scripts) but I know there is an easier way of doing and am hoping that someone can help.

Cheers,

#!/bin/bash
if [ $# -ne 3 ]
then
echo "Usage: 'basename $0' directory file_extension permission"
exit 1
fi

for filename in $(ls $1/*.$2) #this is traversing for matching files in a directory
do
chmod $3 $filename #this is changing the read/write properties
done

exit 0

dxqcanada 09-12-2006 06:09 PM

Re: traverse ... file permissions.
 
From the man page for chmod:

-R, --recursive
Recursively change permissions of directories and their contents.

stress_junkie 09-12-2006 07:58 PM

chmod -R is good if you want to change the permissions of files with names that fit a regular expression. For example, if you can list all of the files that you want to change using the ls command using some expression to match the names of the files then you can select those same files using chmod -R. So if you can do:
Code:

ls -R <expression>
then you can do
Code:

chmod -R <expression>
You might also want to add the -c option to the chmod command so that it will list all of the files that it changes.
Code:

chmod -cR <expression>
If you need to select the files based on other characteristics then you might want to use the find command to select the files and pipe that list to the chmod command. Here's an example that selects all of the files in the current directory tree that have suid set and removes that characteristic.
Code:

find . -type f -perm +6000 -exec chmod -c a-s {} \;
The find command can be very handy when you want to make changes on groups of files.

bskrakes 09-13-2006 09:59 AM

Alright then, making some head way! This helps a lot, I kind of thought recursive would come in handy in this case.

Thanks guys!

bskrakes 09-14-2006 04:38 PM

Man tool
 
Hahahahaha oh man I guess I am still fairly "fresh off the boat," I didn't know of the very handy and cool tool "man." That is amazing and so very helpful!


All times are GMT -5. The time now is 01:35 PM.