chown user:group file.txt:将文件所有者修改为 user,所属组修改为 group
chown root file.txt:将文件所有者修改为 root
chgrp:改变文件的所属组
chgrp group file.txt:将文件所属组修改为 group
UNIX 接口
stat/fstat/lstat 函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<sys/stat.h>
intstat(constchar *restrict pathname, struct stat *restrict statbuf); intfstat(int fd, struct stat *statbuf); intlstat(constchar *restrict pathname, struct stat *restrict statbuf);
DESCRIPTION These functions return information about a file, in the buffer pointed to by statbuf. No permissions are required on the file itself, but—in the case of stat(), fstatat(), and lstat()—execute (search) permission is required on all of the directories in pathname that lead to the file.
RETURN VALUE On success, zero is returned. On error, -1 is returned, and errno is set to indicate the error.
structstat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* Inode number */ mode_t st_mode; /* File type and mode */ nlink_t st_nlink; /* Number of hard links */ uid_t st_uid; /* User ID of owner */ gid_t st_gid; /* Group ID of owner */ dev_t st_rdev; /* Device ID (if special file) */ off_t st_size; /* Total size, in bytes */ blksize_t st_blksize; /* Block size for filesystem I/O */ blkcnt_t st_blocks; /* Number of 512 B blocks allocated */
/* Since POSIX.1-2008, this structure supports nanosecond precision for the following timestamp fields. For the details before POSIX.1-2008, see VERSIONS. */
structtimespecst_atim;/* Time of last access */ structtimespecst_mtim;/* Time of last modification */ structtimespecst_ctim;/* Time of last status change */
#define S_IRWXU 00700 // owner has read, write, and execute permission #define S_IRUSR 00400 // owner has read permission #define S_IWUSR 00200 // owner has write permission #define S_IXUSR 00100 // owner has execute permission
#define S_IRWXG 00070 // group has read, write, and execute permission #define S_IRGRP 00040 // group has read permission #define S_IWGRP 00020 // group has write permission #define S_IXGRP 00010 // group has execute permission
#define S_IRWXO 00007 // others (not in group) have read, write, and execute permission #define S_IROTH 00004 // others have read permission #define S_IWOTH 00002 // others have write permission #define S_IXOTH 00001 // others have execute permission