I have a directory named cache in /var/www, where I need to provide permission in following way:

sudo chmod -R 777 cache

but that's not enough because PHP script automatically creates sub-directories under that for better file management like a, b, c... and so on. Now here whenever any new directory gets created it does not inherit permission from parent directory "cache" instead it stays with drwxr-xr-x access instead of drwxrwxrwx.

Can anyone help?

  • 1
    Google 'php' and 'umask'. Also that's not really Ubuntu related. You can also use php's chmod() function every time the new directory is created or create a cronjob that runs chmod -R 777 var/www/cache.
    – con-f-use
    May 23, 2012 at 11:08
  • I understand but just coined this question here as I was using ubuntu. Next time I will be posting it on common linux question website.
    – deej
    May 23, 2012 at 11:45
  • try `chmod -R 2777 cache', theoretically that first 2, called "a sticky bit" I think, should make files and directories inherit permissions May 23, 2012 at 11:52
  • hm.. tried that.. theoretically it should but I think it's not doing it as expected as of now.
    – deej
    May 23, 2012 at 12:21
  • 1
    That first 2 is called gid, not sticky bit. It only makes the group 'inherit' and nothing else. To inherit permissions, explore the setfacl command.
    – Amit Naidu
    Apr 23, 2013 at 7:36

1 Answer 1


This is the intended behaviour of the UNIX umask feature, which is used to control the permissions of newly created files.

For example, with a umask of 0022, the group and world write bits will be masked out. This likely explains the results you saw with the directories your script creates.

You might be able to adjust the umask in PHP to 0000, but as this is a process global setting and the PHP interpreter is embedded in Apache, it might have security implications beyond your script. The alternative is to issue a chmod() call after creating the directory, which should be a bit safer and doesn't depend on the umask setting.

You might also want to consider whether you really need to make these directories world writeable. If the files need to be writeable by the web server and some other script, perhaps you could set the directory to be owned by a common group for those two environments and only make it group writeable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.