如何在Linux中以编程方式向特定用户授予文件写入权限?例如,文件的所有者?每个人都可以读取此文件。
在shell或shell脚本中,只需使用以下命令:
chmod u+w <filename>
这将仅修改用户的写入位,所有其他标志保持不变。
如果您想在C程序中执行此操作,则需要使用:
int chmod(const char *path, mode_t mode);
首先通过以下方式查询现有模式:
int stat(const char *path, struct stat *buf);
...只需执行newMode = oldMode | S_IWUSR
来设置写入位。请参阅man 2 chmod
和man 2 stat
了解详细信息。
chmod(path, 0666);
。 - Delan Azabanichmod(path, 0644);
,这应该会产生预期的效果。我将“但是,可以被任何其他用户访问和读取”部分解释为重置组和其他模式的绿灯。 - Delan Azabani八进制模式644
将为所有者提供读取和写入权限,以及仅为组中其余成员和其他用户提供读取权限。
read = 4
write = 2
execute = 1
owner = read | write = 6
group = read = 4
other = read = 4
设置模式的基本命令语法为:
chmod 644 [file name]
在C语言中,那将是
#include <sys/stat.h>
chmod("[file name]", 0644);
chmod 644 FILENAME
6是可读写的,4是只读的。假设文件的所有者就是您想要授予写访问权限的用户。
你可以使用chmod命令来实现,如果你在Ubuntu系统上,可以尝试以下命令: $sudo chmod 666 这将赋予所有用户读写权限...更多细节请参考: http://catcode.com/teachmod/
c
,所以一个shell答案并不特别有帮助。 - Toby Speightchmod 777 <filename>
。