LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2008, 04:39 AM   #1
ramesh14
Member
 
Registered: Apr 2008
Location: Hyderabad
Distribution: RHEL5,Fedora 8/9
Posts: 119
Blog Entries: 1

Rep: Reputation: 15
Question scrip to change directory/file permissions


I have a directory which contains some directories and files. and the sub-dirs contains some more dirs and files. All the directories and files inside the main directory are having different permissions. I want to give 0755 for all dirs and 0644 for all files in the main dir. Changing permissions for each and every thing is a critical job. This bash shell script if for a single dir or file (example: polo).
Code:
if [ -d polo ]
   chmod 0755 polo;
elif [ -f polo ]
   chmod 0644 polo;
else
   echo " polo is not a dir or file";
Further i am not understanding. I have to perform this action many time when ever i copy contents. Can any body please help me?
 
Old 09-11-2008, 05:02 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by ramesh14 View Post
I have a directory which contains some directories and files. and the sub-dirs contains some more dirs and files. All the directories and files inside the main directory are having different permissions. I want to give 0755 for all dirs and 0644 for all files in the main dir. Changing permissions for each and every thing is a critical job. This bash shell script if for a single dir or file (example: polo).
Code:
if [ -d polo ]
   chmod 0755 polo;
elif [ -f polo ]
   chmod 0644 polo;
else
   echo " polo is not a dir or file";
Further i am not understanding. I have to perform this action many time when ever i copy contents. Can any body please help me?
This should work. Just substitute /path/to/ by the correct path, and all the files and dirs under that node will be processed.

Code:
find /path/to/ \( -type d -exec echo chmod 0755 '{}' \; -o -type f -exec chmod 0644 '{}' \; \)
If you want, you could as well do a bash function, for example, try putting this into your ~/.bashrc and ~/.bash_profile files:

Code:
# Function to fix permissions
function fixperms () {
  if [ -z "$1" ]
  then
    echo "Usage: fixperms base_directory [file permissions] [dir_permissions]"
    return
  fi
  if [ -z "$2" ]; then local FILE_PERMS=0644; else FILE_PERMS="$2"; fi
  if [ -z "$3" ]; then local DIR_PERMS=0755; else DIR_PERMS="$3"; fi
  echo "* Using $FILE_PERMS for files and $DIR_PERMS for directories."
  if [ -d "$1" ] || [ -f "$1" ]
  then
    find "$1" \( \
      -type d -exec chmod "$DIR_PERMS" '{}' \; -o \
      -type f -exec chmod "$FILE_PERMS" '{}' \; \
    \)
  else
    echo "! $1 not found."
  fi
}
This will be quite a time saver if you do this a lot. Now you can reload your shell (just close your terminal and open a new one, run exec bash or re-login if you wish) so the changes takes effect. From now on, you should be able to just run

Code:
$ fixperms /any/dir/you/want
Probably much easy to handle than the sentence I wrote above.

EDIT: I found an improved version of this function on my home and pasted it instead. It's basically the same with a couple of checks and basic help.

Last edited by i92guboj; 09-11-2008 at 05:54 AM.
 
Old 09-11-2008, 05:37 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Working interactively, it may be easier to use the find command twice.

find /path/to/dir -type d -exec chmod 0755 '{}' \;
find /path/to/dir -type f -exec chmod 0644 '{}' \;

You do want to chmod the directories first in the case where you don't have read or executable rights for a directory. That will allow find to enter directories when you chmod the files.
 
Old 09-11-2008, 09:05 AM   #4
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Smile

Quote:
Originally Posted by jschiwal View Post
find /path/to/dir -type d -exec chmod 0755 '{}' \;
find /path/to/dir -type f -exec chmod 0644 '{}' \;
.
This is the best solution.
 
Old 09-11-2008, 01:35 PM   #5
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by vikas027 View Post
This is the best solution.
no, I don't think so ;-) You're starting a separate process for each file or directory. There's xargs to avoid this:
Code:
find /path/to/dir -type d -print | xargs chmod 0755
find /path/to/dir -type f -print | xargs chmod 0644
And if you have file names including blanks, line feeds and other special characters, use:
Code:
find /path/to/dir -type d -print0 | xargs -0 chmod 0755
find /path/to/dir -type f -print0 | xargs -0 chmod 0644
Jan
 
  


Reply



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
Trying to traverse a directory to change file permissions bskrakes Linux - Newbie 4 09-14-2006 04:38 PM
How do I change directory permissions ? cwolf78 Linux - Software 3 05-05-2005 12:15 PM
can't change directory permissions walterbyrd Debian 7 07-24-2004 01:20 PM
How do I Change directory permissions? Stevetgn Linux - Newbie 8 06-09-2004 12:09 AM
recursive file permissions does not change new files in same directory PAB Linux - Newbie 2 03-08-2004 12:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 AM.

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
Open Source Consulting | Domain Registration