以下是对linux中文件和目录操作的相关函数进行了详细的分析介绍,需要的朋友可以过来参考下
struct stat
{
mode_t st_mode; 文件类型,文件权限
ino_t st_ino; i节点号
dev_t st_dev;
dev_t st_rdev; 设备文件序号
nlink_t st_nlink; 链接
uid_t st_uid;
gid_t st_gid; 用户ID
off_t st_size; 文件大小,此字段只对普通文件、目录文件和符号连接有意义。
time_t st_atime; 最后存取时间
time_t st_mtime; 文件内容的最后修改时间
time_t st_ctime; 文件状态的最后修改时间
long st_blksize;
long st_blocks;
};
1,stat函数取得文件信息。
#include <sys/types.h>
int stat(const char *pathname, struct stat *buf);
int fstat (int fd,struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
lstat函数类似于stat,但是当命名的文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息
2,access函数判断文件权限
#include<unistd.h>
int access (const char *name, int mode) ;
返回:若成功则为 0,若出错则为- 1
access函数的mode常数,取自 <unistd.h>
mode 说 明
R_OK 测试读许可权
W_OK 测试写许可权
X_OK 测试执行许可权
F_OK 测试文件是否存在
3,umask函数设置文件创建屏蔽字
#include <sys/types.h>
mode_t umask(mode_t task) ;
返回:以前的文件方式创建屏蔽字
4,chmod函数用于修改文件的权限
#include <sys/types.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
两个函数返回:若成功则为 0,若出错则为- 1
5,chown函数可用于更改文件的用户 ID和组ID。
#include <sys/types.h>
int chown(const char *pathname,uid_t owner,gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
三个函数返回:若成功则为 0,若出错则为- 1
6,在文件末尾处截短文件可以调用函数 truncate和ftruncate。将一个文件的长度截短为 0是一个特例,用O_TRUNC标志可以做到这一点。
#include <sys/types.h>
int truncate(const char *pathname, off_t
length) ;
int ftruncate(int filedes, off_t length) ;
两个函数返回;若成功则为 0,若出错则为- 1
7,创建一个向现存文件连接的方法是使用link函数,想当于硬连接 ln。只有超级用户进程可以创建指向一个目录的新连接。其理由是这样做可能在文件系统中形成循环,大多数处理文件系统的公用程序都不能处理这种情况
struct stat
{
mode_t st_mode; 文件类型,文件权限
ino_t st_ino; i节点号
dev_t st_dev;
dev_t st_rdev; 设备文件序号
nlink_t st_nlink; 链接
uid_t st_uid;
gid_t st_gid; 用户ID
off_t st_size; 文件大小,此字段只对普通文件、目录文件和符号连接有意义。
time_t st_atime; 最后存取时间
time_t st_mtime; 文件内容的最后修改时间
time_t st_ctime; 文件状态的最后修改时间
long st_blksize;
long st_blocks;
};
1,stat函数取得文件信息。
#include <sys/types.h>
int stat(const char *pathname, struct stat *buf);
int fstat (int fd,struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
lstat函数类似于stat,但是当命名的文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息
2,access函数判断文件权限
#include<unistd.h>
int access (const char *name, int mode) ;
返回:若成功则为 0,若出错则为- 1
access函数的mode常数,取自 <unistd.h>
mode 说 明
R_OK 测试读许可权
W_OK 测试写许可权
X_OK 测试执行许可权
F_OK 测试文件是否存在
3,umask函数设置文件创建屏蔽字
#include <sys/types.h>
mode_t umask(mode_t task) ;
返回:以前的文件方式创建屏蔽字
4,chmod函数用于修改文件的权限
#include <sys/types.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
两个函数返回:若成功则为 0,若出错则为- 1
5,chown函数可用于更改文件的用户 ID和组ID。
#include <sys/types.h>
int chown(const char *pathname,uid_t owner,gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
三个函数返回:若成功则为 0,若出错则为- 1
6,在文件末尾处截短文件可以调用函数 truncate和ftruncate。将一个文件的长度截短为 0是一个特例,用O_TRUNC标志可以做到这一点。
#include <sys/types.h>
int truncate(const char *pathname, off_t
length) ;
int ftruncate(int filedes, off_t length) ;
两个函数返回;若成功则为 0,若出错则为- 1
7,创建一个向现存文件连接的方法是使用link函数,想当于硬连接 ln。只有超级用户进程可以创建指向一个目录的新连接。其理由是这样做可能在文件系统中形成循环,大多数处理文件系统的公用程序都不能处理这种情况
int link(const char*oldpath, const char *newpath) ;
返回:若成功则为 0,若出错则为- 1
为了删除一个现存的目录项,可以调用 unlink函数。
int unlink(const char *pathname) ;
返回:若成功则为 0,若出错则为-1。此函数删除目录项,并将由 pathname所引用的文件的连接计数减 1。
硬连接的一些限制: ( a )硬连接通常要求连接和文件位于同一文件系统中, ( b )只有超级用户才能创建到目录的硬连接。
symlink函数创建一个符号连接。相当于软连接,ln -s
int symlink(const char *oldpath, const char *sympath) ;
返回:若成功则为 0,若出错则为- 1
因为open函数跟随符号连接,所以需要有一种方法打开该连接本身,并读该连接中的名字。
readlink函数提供了这种功能。
int readlink(const char *pathname, char *buf, int bufsize) ;
返回:若成功则为读的字节数,若出错则为- 1
此函数组合了open, read和close的所有操作。
8,用mkdir函数创建目录,用 rmdir函数删除目录。
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode) ;
返回:若成功则为 0,若出错则为- 1
#include <unistd.h>
int rmdir(const char *pathname) ;
返回:若成功则为 0,若出错则为 - 1
9,remove函数解除对一个文件或目录的连接。对于文件, remove的功能与unlink相同。对于目录, remove的功能与rmdir相同。
#include <stdio.h>
int remove(const char *pathname) ;
返回:若成功则为 0,若出错则为- 1
文件或目录用rename函数更名。
int rename(const char *oldname, const char *newwname) ;
返回:若成功则为 0,若出错则为- 1
10,一个文件的存取和修改时间可以用 utime函数更改。
#include <sys/types.h>
int utime (const char *name, const struct utimebuf *t);
返回:若成功则为 0,若出错则为- 1
如果times是一个空指针,则存取时间和修改时间两者都设置为当前时间;
如果times是非空指针,则存取时间和修改时间被设置为 times所指向的结构中的值。此时,进程的有效用户ID必须等于该文件的所有者 ID,或者进程必须是一个超级用户进程。对文件只具有写许可权是不够的
此函数所使用的结构是:
struct utimbuf {
time_t actime; /*access time*/
time_t modtime; /*modification time*/
}
11,对文件目录的操作函数,opendir readdir rewinddir
#include <sys/types.h>
DIR *opendir(const char *pathname) ;
返回:若成功则为指针,若出错则为 NULL
struct dirent *readdir(DIR *dr);
返回:若成功则为指针,若在目录尾或出错则为 NULL
void rewinddir(DIR *dr);
重置读取目录的位置为开头
int close(DIR *dr); 返回:若成功则为 0,若出错则为- 1
定义在头文件<dirent.h>中的dirent结构与实现有关。 此结构至少包含下列两个成员:
struct dirent {
ino_t d_ino;
char d_name[NAME_MAX+1];
}
12,chdir,改变当前目录
#include<unistd.h>
int chdir(const char *pathname);
int pchdir(int fd);
getcwd,得到当前目录的完整路径.
char *getcwd(char *buf, size_t size);
若失败返回NULL, buf为存储路径的字符数组,size为长度
int link(const char*oldpath, const char *newpath) ;
返回:若成功则为 0,若出错则为- 1
为了删除一个现存的目录项,可以调用 unlink函数。
int unlink(const char *pathname) ;
返回:若成功则为 0,若出错则为-1。此函数删除目录项,并将由 pathname所引用的文件的连接计数减 1。
硬连接的一些限制: ( a )硬连接通常要求连接和文件位于同一文件系统中, ( b )只有超级用户才能创建到目录的硬连接。
symlink函数创建一个符号连接。相当于软连接,ln -s
int symlink(const char *oldpath, const char *sympath) ;
返回:若成功则为 0,若出错则为- 1
因为open函数跟随符号连接,所以需要有一种方法打开该连接本身,并读该连接中的名字。
readlink函数提供了这种功能。
int readlink(const char *pathname, char *buf, int bufsize) ;
返回:若成功则为读的字节数,若出错则为- 1
此函数组合了open, read和close的所有操作。
8,用mkdir函数创建目录,用 rmdir函数删除目录。
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode) ;
返回:若成功则为 0,若出错则为- 1
#include <unistd.h>
int rmdir(const char *pathname) ;
返回:若成功则为 0,若出错则为 - 1
9,remove函数解除对一个文件或目录的连接。对于文件, remove的功能与unlink相同。对于目录, remove的功能与rmdir相同。
#include <stdio.h>
int remove(const char *pathname) ;
返回:若成功则为 0,若出错则为- 1
文件或目录用rename函数更名。
int rename(const char *oldname, const char *newwname) ;
返回:若成功则为 0,若出错则为- 1
10,一个文件的存取和修改时间可以用 utime函数更改。
#include <sys/types.h>
int utime (const char *name, const struct utimebuf *t);
返回:若成功则为 0,若出错则为- 1
如果times是一个空指针,则存取时间和修改时间两者都设置为当前时间;
如果times是非空指针,则存取时间和修改时间被设置为 times所指向的结构中的值。此时,进程的有效用户ID必须等于该文件的所有者 ID,或者进程必须是一个超级用户进程。对文件只具有写许可权是不够的
此函数所使用的结构是:
struct utimbuf {
time_t actime; /*access time*/
time_t modtime; /*modification time*/
}
11,对文件目录的操作函数,opendir readdir rewinddir
#include <sys/types.h>
DIR *opendir(const char *pathname) ;
返回:若成功则为指针,若出错则为 NULL
struct dirent *readdir(DIR *dr);
返回:若成功则为指针,若在目录尾或出错则为 NULL
void rewinddir(DIR *dr);
重置读取目录的位置为开头
int close(DIR *dr); 返回:若成功则为 0,若出错则为- 1
定义在头文件<dirent.h>中的dirent结构与实现有关。 此结构至少包含下列两个成员:
struct dirent {
ino_t d_ino;
char d_name[NAME_MAX+1];
}
12,chdir,改变当前目录
#include<unistd.h>
int chdir(const char *pathname);
int pchdir(int fd);
getcwd,得到当前目录的完整路径.
char *getcwd(char *buf, size_t size);
若失败返回NULL, buf为存储路径的字符数组,size为长度
举例:
函数原型:
#include <sys/stat.h>
int mkdir(const char *path, mode_t mode);
参数:
path是目录名
mode是目录权限
返回值:
返回0 表示成功, 返回 -1表示错误,并且会设置errno值。
mode模式位:
mode 表示新目录的权限,可以取以下值:
S_IRUSR
S_IREAD
S_IWUSR
S_IWRITE
S_IXUSR
S_IEXEC
S_IRWXU
This is equivalent to (S_IRUSR | S_IWUSR | S_IXUSR).
S_IRGRP
Read permission bit for the group owner of the file. Usually 040.
S_IWGRP
Write permission bit for the group owner of the file. Usually 020.
S_IXGRP
Execute or search permission bit for the group owner of the file. Usually 010.
S_IRWXG
This is equivalent to (S_IRGRP | S_IWGRP | S_IXGRP).
S_IROTH
Read permission bit for other users. Usually 04.
S_IWOTH
Write permission bit for other users. Usually 02.
S_IXOTH
Execute or search permission bit for other users. Usually 01.
S_IRWXO
This is equivalent to (S_IROTH | S_IWOTH | S_IXOTH).
S_ISUID
This is the set-user-ID on execute bit, usually 04000. See How Change Persona.
S_ISGID
This is the set-group-ID on execute bit, usually 02000. See How Change Persona.
S_ISVTX
This is the sticky bit, usually 01000.
例子:
#include <sys/types.h> #include <sys/stat.h>
int status;
status = mkdir(“/home/newdir”, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
这样就创建了一个newdir目录,权限通过ls -al 查看为
drwxr-xr-x
跟用linux命令mkdir创建的目录权限位一致。
其他参考:
=======================================================
mkdir的函数原型(使用时需包含#include <sys/stat.h>):
int mkdir(const char *path, mode_t mode);
参数:
path——目录名,比如abc,/var/www/abc等
mode——目录权限
返回值:
返回0 表示成功, 返回 -1表示错误,并且会设置errno值。
关于Mode定义,请参考:http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html
当然你也可以不用那些类似S_IRWXU、S_IRUSR…类似的宏参数,毕竟很难记忆,反倒不如8进制的0421好记。组合样式为:owner-group-others,不同人都分三个规则读-写-执行(r-w-x),全部许可就是7。
1、编程时使用mkdir(“test”,777)报错
试一下:
mkdir(“test”,0777);
写成mkdir(“test”,777)有可能无法执行。但是依稀记得,以前都是用777的也没错误,具体忘记了,反正按照规范写绝对没错。
2、umask命令使用
另外假设你的程序目录在/root/abc/下,那么如果你的程序执行时想在/var/www下创建目录时,有可能你始终创建一个0777的目录总是创建成0755目录,这时可以尝试使用umask(0)命令。
umask只对当前目录有效,默认umask值为0022,所以你无法在另外一个地方直接创建0777的目录。
而是0777-0022=0755
3、如何创建某个用户组的文件夹
/etc/passwd和/etc/group找到UID和gid
mkdir /var/ugroup
我们可以直接使用chown来改变文件所有者。
chown root:newuser /var/ugroup
修改权限
chmod 740 /var/ugroup/*
4、察看创建后目录权限情况命令:
定位到该用户组目录下,执行:
ls -all
会显示类似:
drwxrwxr-x
这样的结果(0775)。
=======================================================