permissions to create file in directory [duplicate]

I've read here that in order to create a file inside a directory in Linux, one should have write permission to that directory. However when examining this (in Ubuntu Server 14) it did not worked.

osboxes@osboxes:~$ mkdir mydir
osboxes@osboxes:~$ chmod 200 mydir
osboxes@osboxes:~$ ls -l
d-w------- 2 osboxes osboxes 4096 May 25 09:52 mydir
osboxes@osboxes:~$ touch mydir/myfile
touch: cannot touch 'mydir/myfile': Permission denied

only after adding execute permissionto mydir I could touch that file. So execute permissions are also needed in order to touch a file?

EDIT: from @Pablo Bianchi ref I learn that

Write (w)

The ability to rename files in the directory, create new files, or delete existing files, if you also have Execute permissions. If you don't have execute perms, then write perms are meaningless."

So I guess that execute perms are ndeed required in order to use write perms to create new files.

4

2 Answers

A directory is nothing else than a file, but it is a special file which contains a list of hardlinks to the files and subdirectories (which are also indeed files).

If you have read permissions for a directory, you are able to read the list of hardlinks.

If you have write permissions for a directory you are able to modify the list of hardlinks.

The execute permission is like a door to the next level, if you have execute permission, the door is open. If you don't have execute permission, the door is closed.

If you want to create a file in a directory, you need to be able to modify the list of hardlinks, thus you need write permission for the directory. But that's just a directory-entry, the file itself will reside behind the door, so you need execute permission to open the door.

Imagine a room with a table inside it. On the door is a list with the furniture in the room, the list contains only the word 'table'. Now you want to add a chair to the table in the room. You add the word 'chair' to the list on the door. And you need to open the door to bring the chair inside.

1

From the start, after creating your directory, you should have all the persimissions for yourself to read write and execute.

Right after mkdir if you use ls -l command the output should be like this

drwxr-xr-x 2 osboxes osboxes 4096 May 25 09:52 mydir

In order to return to that configuration you could run :

chmod 755 mydir

If you just want to remove all permsissions to anyone but you

chmod go-rx mydir

It could be useful to read more about Understanding Linux Permissions and chmod Usage

1

You Might Also Like