boom
boom

Reputation: 6166

How do I give write permission to file in Linux?

How can I (programmatically) give write permission on a file to a particular user in Linux? Like, for example, its owner? Everyone has read access to this file.

Upvotes: 21

Views: 90975

Answers (6)

Enlem Enlem
Enlem Enlem

Reputation: 107

in shell script, if you want to grant access to the user "john" and they belong to the group "users," you would run:

sudo chown john:users example.txt

Upvotes: -1

Kumar
Kumar

Reputation: 452

You can use chmod 777 <filename>

Upvotes: -2

DarkDust
DarkDust

Reputation: 92336

In a shell or shell script simply use:

chmod u+w <filename>

This only modifies the write bit for the user, all other flags remain untouched.

If you want to do it in a C program, you need to use:

int chmod(const char *path, mode_t mode);

First query the existing mode via

int stat(const char *path, struct stat *buf);

... and just set the write bit by doing newMode = oldMode | S_IWUSR. See man 2 chmod and man 2 stat for details.

Upvotes: 31

Delan Azabani
Delan Azabani

Reputation: 81384

The octal mode 644 will give the owner read and write permissions, and just read permissions for the rest of the group, as well as other users.

read     = 4
write    = 2
execute  = 1
owner    = read | write = 6
group    = read         = 4
other    = read         = 4

The basic syntax of the command to set the mode is

chmod 644 [file name]

In C, that would be

#include <sys/stat.h>

chmod("[file name]", 0644);

Upvotes: 13

Saurabh Gandhi
Saurabh Gandhi

Reputation: 361

You can do that using chmod, on ubuntu you can try out $sudo chmod 666 This would give read/write permissions to all...check this out for more details: http://catcode.com/teachmod/

Upvotes: -1

Gazler
Gazler

Reputation: 84140

chmod 644 FILENAME

6 is read and write, 4 is read only. Assuming the owner of the file is the user you wish to grant write access to.

Upvotes: 2

Related Questions