LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Security (https://www.linuxquestions.org/questions/linux-security-4/)
-   -   chmod 775 to only the directories and chmod 664 to only the files? (https://www.linuxquestions.org/questions/linux-security-4/chmod-775-to-only-the-directories-and-chmod-664-to-only-the-files-587544/)

apachenew 09-26-2007 11:45 AM

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.

stress_junkie 09-26-2007 11:52 AM

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.

apachenew 09-27-2007 09:31 AM

Awesome! Thanks!

Gethyn 09-27-2007 09:56 AM

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.

stress_junkie 09-27-2007 09:57 AM

Quote:

Originally Posted by Gethyn (Post 2905474)
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?

Gethyn 09-27-2007 10:09 AM

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.

stress_junkie 09-27-2007 03:26 PM

Quote:

Originally Posted by Gethyn (Post 2905489)
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).



All times are GMT -5. The time now is 08:24 PM.