2

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
  • @PabloBianchi not exectly. I understood from it that the x bit on directories is considered to be as a "search bit" or "traverse bit", i.e needed to be set when we want to read some file in it. But I just want to create a new file (not caring of existing files in it). Why x is needed on mydir when I want to touch mydir/myfile ?
    – user112112
    May 26 '20 at 15:58
  • To start with, which Linux distro have you installed (Ubuntu server, Ubuntu desktop, Kubuntu, Lubuntu, Xubuntu, Ubuntu MATE, Mint, et al.), & which release number? Different releases have different tools for us to recommend. Please click edit & add that to your question, so all facts we need are in the question. Please don't use Add Comment, since that's our one-way channel to you. All facts about your PC should go in the Question with edit as this is a Q&A site, not a general forum, so things work differently here.
    – K7AAY
    May 26 '20 at 16:46
  • x bit is not a "search bit", it means the ability to access the content of the files in the directory. The r bit may be more related to a search bit, since is the one that let you list the filenames. May 26 '20 at 19:20
  • "But I just want to create a new file (not caring of existing files in it)." You don't, but the system has to care whether the file exists or not.
    – muru
    May 27 '20 at 4:03
0

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.

0

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
  • yes I know I have sufficient permissions uppon mkdir, I am just exploring unix permissions and wondering why execute permission is needed in order to create files in a directory.
    – user112112
    May 26 '20 at 15:54

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