LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices

Reply
 
Search this Thread
Old 09-26-2007, 11:45 AM   #1
apachenew
Member
 
Registered: Jul 2007
Posts: 30

Rep: Reputation: 15
chmod 775 to only the directories and chmod 664 to only the files?


Hello,

Is there a way to apply chmod 775 to only the directories
and apply chmod 664 to only the files where there a mix of directories and files?
I don't want to do chmod * 775 which will make all the files executable as well.
 
Old 09-26-2007, 11:52 AM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
If you are in the parent directory of the files and directories that you want to change then the following commands will do the job.

For directories only do this.
Code:
find . -type d -exec chmod 775 {} \;
For files only do this.
Code:
find . -type f -exec chmod 664 {} \;
Here you can see that we are selecting which files to affect by using a parameter that selects files by type. The d is for directories and the f is for regular files.

The command for the directories will include the . and the .. directories where you issue the command. Make sure that you check the permissions on your current directory and its parent after you issue the commands.

You can avoid this by providing a wildcard in the command to select the directories. The following command will not affect the . and the .. directories because it does not affect any files whose names begin with a dot.
Code:
find . -type d -name \* -exec chmod 775 {} \;
The find command is the Swiss Army Knife of file manipulation. It has a lot of parameters to select files by lots of different criteria. Then you can use the -exec parameter to send the list of selected files to any other program.

Last edited by stress_junkie; 09-26-2007 at 12:00 PM.
 
1 members found this post helpful.
Old 09-27-2007, 09:31 AM   #3
apachenew
Member
 
Registered: Jul 2007
Posts: 30

Original Poster
Rep: Reputation: 15
Awesome! Thanks!
 
Old 09-27-2007, 09:56 AM   #4
Gethyn
Member
 
Registered: Aug 2003
Location: UK
Distribution: (X)Ubuntu 10.04/10.10, Debian 5, CentOS 5
Posts: 900

Rep: Reputation: 32
If none of the files are set +x at the moment, you could also use the command 'chmod -R +X' (note upper case X). I think this adds execute permission to directories but not files. For whatever reason it doesn't seem to work the other way round, i.e. -X.
 
Old 09-27-2007, 09:57 AM   #5
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by Gethyn View Post
If none of the files are set +x at the moment, you could also use the command 'chmod -R +X' (note upper case X). I think this adds execute permission to directories but not files. For whatever reason it doesn't seem to work the other way round, i.e. -X.
Sorry Gethyn but that command would add execute permission to all of the files. Why would you think it somehow magically limited itself to directories?
 
Old 09-27-2007, 10:09 AM   #6
Gethyn
Member
 
Registered: Aug 2003
Location: UK
Distribution: (X)Ubuntu 10.04/10.10, Debian 5, CentOS 5
Posts: 900

Rep: Reputation: 32
Well, according to the manpage for chmod (e.g. here), the option +x (lowercase) adds execute permission to anything, whereas the option +X (uppercase) adds execute permission only if the target is a directory, and does nothing when the target is not a directory. Nothing magical about that. Should've checked that before I posted though, the reason that it does nothing that -X (uppercase) doesn't work is that it's not supposed to, the man page says it's ignored in all cases except +X.
 
Old 09-27-2007, 03:26 PM   #7
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by Gethyn View Post
Well, according to the manpage for chmod (e.g. here), the option +x (lowercase) adds execute permission to anything, whereas the option +X (uppercase) adds execute permission only if the target is a directory, and does nothing when the target is not a directory. Nothing magical about that. Should've checked that before I posted though, the reason that it does nothing that -X (uppercase) doesn't work is that it's not supposed to, the man page says it's ignored in all cases except +X.
Okay you got me. I should have checked the man page as well.
Quote:
Originally Posted by man page
The letters ‘rwxXstugo' select the new permissions for the affected
users: read (r), write (w), execute (or access for directories) (x),
execute only if the file is a directory or already has execute permis‐
sion for some user (X), set user or group ID on execution (s), sticky
(t), the permissions granted to the user who owns the file (u), the
permissions granted to other users who are members of the file's group
(g), and the permissions granted to users that are in neither of the
two preceding categories (o).
 
  


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
[SOLVED] FTP: recursive chmod (separate for directories and files) mgmax Linux - Software 11 01-29-2011 08:06 AM
Using chmod to recursively change directories / files [GOD]Anck Linux - Software 6 11-10-2008 06:16 PM
Chown/Chmod ff Directories/Files Question Genin Linux - Newbie 4 12-27-2006 11:51 AM
Recursive chmod with different values for files and directories? Z038 Linux - Newbie 2 06-07-2006 08:38 AM
chmod directories and files seperately robeb Linux - General 2 05-23-2003 08:01 PM


All times are GMT -5. The time now is 05:04 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration