LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-12-2006, 06:07 PM   #1
bskrakes
Member
 
Registered: Sep 2006
Location: Canada, Alberta
Distribution: RHEL 4 and up, CentOS 5.x, Fedora Core 5 and up, Ubuntu 8 and up
Posts: 251

Rep: Reputation: 32
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
 
Old 09-12-2006, 07:09 PM   #2
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
Re: traverse ... file permissions.

From the man page for chmod:

-R, --recursive
Recursively change permissions of directories and their contents.
 
Old 09-12-2006, 08:58 PM   #3
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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.

Last edited by stress_junkie; 09-12-2006 at 09:01 PM.
 
Old 09-13-2006, 10:59 AM   #4
bskrakes
Member
 
Registered: Sep 2006
Location: Canada, Alberta
Distribution: RHEL 4 and up, CentOS 5.x, Fedora Core 5 and up, Ubuntu 8 and up
Posts: 251

Original Poster
Rep: Reputation: 32
Smile

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

Thanks guys!
 
Old 09-14-2006, 05:38 PM   #5
bskrakes
Member
 
Registered: Sep 2006
Location: Canada, Alberta
Distribution: RHEL 4 and up, CentOS 5.x, Fedora Core 5 and up, Ubuntu 8 and up
Posts: 251

Original Poster
Rep: Reputation: 32
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script tar+gzip traverse a directory gn00kie Programming 7 06-16-2006 02:52 AM
How do I change directory permissions ? cwolf78 Linux - Software 3 05-05-2005 01:15 PM
can't change directory permissions walterbyrd Debian 7 07-24-2004 02:20 PM
recursive file permissions does not change new files in same directory PAB Linux - Newbie 2 03-08-2004 01:27 PM
is there a way to traverse a directory structure & execute a command hemlock Linux - Newbie 1 09-07-2002 01:22 AM


All times are GMT -5. The time now is 06:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration