Reputation: 6166
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
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
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
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
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
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