Why are execute permissions along with write permissions needed for creating a file in C (Linux)?

Posted on

Problem :

I am trying to create a file from my C source code on linux. I am using the dd command for reading from a file and writing it to a file by creating it. If I don’t have execute permissions (I have write permissions) for the target directory (where the file is going to be created), it is unable to create the file. If I have both write and execute permissions, it is able to create file.

What is the reason for this?

Solution :

When set, the r, w, and x mode attributes have the following effect on files and
directories:

  • r Attribute (read permission):

Files: Allows a file to be opened and read.

Directories: Allows a directory’s contents to be listed. To retrieve any info about the containing files or directories, the execute attribute must also be set.

  • w Attribute (write permission):

Files: Allows a file to be written to or truncated, however this attribute does not allow files to be renamed or deleted. The ability to delete or rename files is determined by directory attributes.

Directories: Allows files within a directory to be created, deleted, and renamed if the execute attribute is also set.

  • x Attribute (execute permission):

Files: Allows a file to be treated as a program and executed. Program files written in scripting languages must also be set as readable to be executed.

Directories: Allows a directory to be entered, e.g., cd directory.

Reference: The Linux® Command Line William E. Shotts, Jr.

I would recommend to read this book or pdf version which you can download it from this link: http://sourceforge.net/projects/linuxcommand/files/TLCL/13.07/TLCL-13.07.pdf/download

The execution bit on a directory grants you the possibility to enter/trasverse it. Therefore it’s a prerequisite to create a file (otherwise you can enter it) aswell in your case.

Leave a Reply

Your email address will not be published.