Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: Set permission only to directories [closed]

I have to change the permissions of the htdocs directory in apache to a certain group and with certain read/write/execute.

The directories need to have 775 permissions and the files need to have 664.

If I do a recursive 664 to the htdocs, then all files and directories will change to 664.

I don't want to change the directories manually.

Is there any way to change only files or directories?

like image 847
radicaled Avatar asked Jun 13 '13 15:06

radicaled


People also ask

How do I chmod only a folder?

Use chmod +X (as opposed to +x ) which will make only directories executable and leave files alone. That will make all your files writeable by you and the server, but not executable, but will also make directories executable (i.e., listable).

How do you change permissions in Linux for all files in a folder?

We can use symbolic code plus (+) to add permissions and use minus (–) to remove permissions. Therefore, to give read permission we use +r. In addition, we use +w to give write permission and +x to give execute permission.

What is the meaning of chmod 777?

Setting 777 permissions to a file or directory means that it will be readable, writable and executable by all users and may pose a huge security risk.


6 Answers

chmod can actually do this itself; the X symbolic permission means "execute, if it makes sense" which generally means on directories but not files. So, you can use:

chmod -R u=rwX,go=rX /path/to/htdocs

The only potential problem is that if any of the plain files already have execute set, chmod assumes it's intentional and keeps it. If this is a potential problem and you have the GNU version of chmod (i.e. you're on Linux), you can get it to remove any stray execute permissions like this:

chmod -R a-x,u=rwX,go=rX /path/to/htdocs

Unfortunately, this trick doesn't work with the bsd (/macOS) version of chmod (I'm not sure about other versions). This is because the bsd version applies the X permission based on "the original (unmodified) mode", i.e. whether it had any execute bits before the a-x modification was done (see the man page).

like image 98
Gordon Davisson Avatar answered Oct 11 '22 15:10

Gordon Davisson


Use find's -type option to limit actions to files and directories. Use the -o option to specify alternate actions for different types, so you only have to run find once, rather than separately for each type.

find htdocs -type f -exec chmod 664 {} + -o -type d -exec chmod 775 {} +
like image 28
Barmar Avatar answered Oct 11 '22 16:10

Barmar


Use find to search for directories and apply chmod on them:

find -type d | xargs chmod 775

Use type f for file:

find -type f | xargs chmod 775
like image 29
Amit Verma Avatar answered Oct 11 '22 16:10

Amit Verma


I use something similar to the solution provided by Gordon:

chmod -R ug=rw,o=r,a+X /path/to/folder/

It should always set 775 for folders and 664 for files, even if the execute permission was previosly set for some file

like image 21
genna Avatar answered Oct 11 '22 17:10

genna


Gordon's answer above is correct, but if you're trying to lock down access to a directory tree, it leaves scripts that are executable to the owner also executable to whoever has been granted the capital X.

Using

find <path> -type d -exec chmod 775 {} +

or

find <path> -type d -exec chmod 755 {} +

is safer.

like image 38
MartinMlima Avatar answered Oct 11 '22 15:10

MartinMlima


try:

find htdocs -type d -exec chmod 775 {} +
like image 30
RicardoE Avatar answered Oct 11 '22 17:10

RicardoE