4

I'm trying to create a sub directory under existing directory tree. I want to know if only the permissions of directory where I'll be creating my sub directory matter or the parent directories will also have some effect on the permission to create the directory?

I'll be doing this programmatically, so I need to be sure that I have covered a wide range of scenarios.

1
  • 1
    Why don't you try it? Create different directories with different permissions and see how they effect your ability to create a sub-directory. Should take you a few minutes.
    – slowko
    Dec 21 '16 at 11:53
9

Yes, they matter. To create a directory, you need to be able to write to its parent directory. Creating a directory is just like creating a file (after all, everything is a file) so you need write access to the parent. In addition, you need to be able to get to the parent directory which means you need execute access to all directories in the tree:

$ sudo tree -pgu 
.
└── [drwxr-xr-x terdon terdon]  dir1
    └── [drwx------ bob      bob     ]  dir2
        └── [drwxr-xr-x terdon terdon]  dir3

    

In the example above, dir2 is owned by bob. This means I cannot cd into it, and I can't cd into its subdirectory dir3 either:

$ cd dir1/dir2/
bash: cd: dir1/dir2/: Permission denied
$ cd dir1/dir2/dir3
bash: cd: dir1/dir2/dir3: Permission denied

    

If I give myself execute access to dir2, I will be able to move to both dir2 and dir2/dir3, but I still won't have the right to create files/directories in dir2:

$ sudo tree -pgu 
.
└── [drwxr-xr-x terdon terdon]  dir1
    └── [drwx--x--x bob      bob     ]  dir2
        └── [drwxr-xr-x terdon terdon]  dir3

$ cd dir1/dir2/
$ ls
ls: cannot open directory '.': Permission denied
$ touch file
touch: cannot touch 'file': Permission denied

As you can see above, while I can move into the directory, I can't list its contents because I don't have read access to it and I can't create anything there because I don't have write access. 

So, to be able to create a new file or directory inside a directory you need:

  • Execute permissions on every parent directory of your target directory.

  • Execute and write permissions for the target directory. 

1
  • Exactly what I wanted. I could not figure out that execute permission is required for navigation. Thanks.
    – sadiq.ali
    Dec 21 '16 at 14:23

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.