Linux系统基础(目录相关指令)

一、sync/fsync/fdatasync

1、硬盘上一般会有些缓存区以此来提高数据的写入效率,操作系统写入数据其实只是写入了缓存区,直到缓存区满才排队写入硬盘中
2、这种操作降低了写入的次数,但是提高了数据写入的延时,导致缓冲区中的数据与磁盘中的数据不同步。
int sync(void);
功能:把所有缓冲区中的数据全部同步到磁盘的命令,并不等待执行完毕才返回,而是命令发布后立即返回
int fsync(int fd);
功能:指定fd文件的缓冲区数据同步到磁盘 针对于一个文件,数据存储到磁盘后才返回
int fdatasync(int fd);
功能:指定fd文件的缓冲区数据同步到磁盘,但是仅是文件数据不同步文件属性

二、fcntl

int fcntl(int fd, int cmd, … /* arg */ )
注意:这是一个变长参数的函数
fd:文件描述符
cmd:操作指令,不同的操作指令决定后续参数的个数和类型
cmd:F_DUPFD (long):
int fcntl(int fd, int F_DUPFD, long newfd );
功能是复制fd,与fd操作同一个文件
返回值:如果已经没有使用则返回newfd,如果newfd已经使用或被占用,则返回一个不小于newfd的文件描述符
F_GETFD (void)/F_SETFD (long)
int fcntl(int fd, int F_GETFD, void/long);
功能:设置或获取文件描述符
返回值:0/1 打开或者不打开,目前只能设置FD_CLOEXEC
0是新进程中保持打开状态,1则是关闭

F_GETFL (void)/F_SETFL (long)
功能:获取文件状态标志(当前文件打开的权限和打开的方式)
F_GETFL:
O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC不能获取
返回值:带有文件状态标志的int类型常量,需要与各标志相与得到
F_SETFL (long)
仅能设置O_APPEND, O_ASYNC,O_DIRECT, O_NOATIME, and O_NONBLOCK
int fcntl(int fd, int F_DUPFD, struct flock*);
功能:为文件加把锁,能锁整个文件或者锁定一部分文件
F_SETLK (struct flock*)
设置锁
F_SETLKW (struct flock*)
测试锁
F_GETLK (struct flock* )
获取锁的信息
注意:加锁并不能让其他进程打不开文件或不能操作,而是使用者都要遵守锁的约定,确保文件不混乱(劝诫锁)
struct flock {

short l_type; /* 锁的类型Type of lock: F_RDLCK,F_WRLCK, F_UNLCK /
short l_whence; /
基础位置How to interpret l_start:SEEK_SET, SEEK_CUR, SEEK_END /
off_t l_start; /
偏移值Starting offset for lock /
off_t l_len; /
锁的长度为0表示锁到文件尾Number of bytes to lock /
pid_t l_pid; /
加锁进程号PID of process blocking our lock
(F_GETLK only) */

};
读锁与读锁成功
读锁与写锁失败
写锁与写锁失败

三、stat/fstat/lstat

功能:用来获取文件属性,返回值:成功返回0,失败返回-1
int stat(const char path, struct stat buf);
需要文件路径
int fstat(int fd, struct stat buf);
需要文件描述符
int lstat(const char path, struct stat buf);
stat/fstat会跟踪链接目标,而lstat不跟踪链接目标
struct stat {
dev_t st_dev; /
ID of device containing file 设备ID
/
ino_t st_ino; /
inode number 节点号
/
mode_t st_mode; /
protection 文件类型和权限*/
nlink_t st_nlink; /* number of hard links 硬链接数*/
uid_t st_uid; /* user ID of owner 用户ID*/
gid_t st_gid; /* group ID of owner 组ID*/
dev_t st_rdev; /* device ID (if special file)特殊设备ID /
off_t st_size; /
total size, in bytes 文件的总字节数*/
blksize_t st_blksize; /* blocksize for file system I/O IO块*/
blkcnt_t st_blocks; /* number of 512B blocks allocated 512字节的块占用多少个*/
time_t st_atime; /* time of last access 最后访问时间*/
time_t st_mtime; /* time of last modification 最后修改时间*/
time_t st_ctime; /* time of last status change 最后文件属性修改时间*/
};
S_ISREG(m) //测试是不是标准文件is it a regular file?
S_ISDIR(m) //目录directory?
S_ISCHR(m) //字符设备文件character device?
S_ISBLK(m) //块设备文件block device?
S_ISFIFO(m) //管道文件FIFO (named pipe)?
S_ISLNK(m) //链接文件symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) //socket文件 (Not in POSIX.1-1996.)

S_IFMT 0170000 获取文件类型出错
S_IFSOCK 0140000 socket文件
S_IFLNK 0120000 软链接
S_IFREG 0100000 标准文件
S_IFBLK 0060000 块设备
S_IFDIR 0040000 目录
S_IFCHR 0020000 字符设备文件
S_IFIFO 0010000 管道文件
S_ISUID 0004000 设置UID字节位
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
S_IRWXU 00700 属主的读写执行权限
S_IRUSR 00400 读权限
S_IWUSR 00200 写权限
S_IXUSR 00100 属主的执行权限

S_IRWXG 00070 属组读写执行权限
S_IRGRP 00040 属组读权限
S_IWGRP 00020 属组写权限
S_IXGRP 00010 属组执行权限

S_IRWXO 00007 其他用户读写执行权限
S_IROTH 00004 其他用户读权限
S_IWOTH 00002 其他用户写执行权限
S_IXOTH 00001 其他用户执行权限

时间转换成字符串 localtime
struct tm *localtime(const time_t *timep);
功能:使用一个记录秒数据的变量,获取当前时间

四、access

int access(const char *pathname ,int mode)
功能:测试当前用户对文件的访问权限,或者文件是否存在
mode:
F_OK:是否存在
R_OK:读权限
W_OK:写权限
X_OK:执行权限
返回

五、umask

mode_t umask(mode_t mask)
功能:设置并获取权限屏蔽码,功能与umask命令一样
返回值:旧的权限屏蔽码

注意:该权限屏蔽码只对当前进程有效,进程退出后就会变成默认值。一旦设置成功,新创
建的文件就不会有mask的权限

六、chmod/fchmod

修改文件的权限
成功返回0,失败返回-1;
其设置的权限不受权限屏蔽码的干扰
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);

七、truncate/ftruncate

功能:修改文件大小,如果文件的,成功返回0,失败返回-1
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);

八、link/unlink/remove/move

int link(const char *old9path, const char *newpath);
创建硬链接,硬链接指向的是文件内存,当链接目标被删除后,依旧可以文件内容
int unlink(const char *pathname);
删除硬链接
注意:普通文件就是硬链接数为1的文件,当把一个文件的硬链接数删除到0时,文件就被删除了
int remove(const char *pathname);
功能:删除文件,该函数是标准库中的删除文件函数,底层调用了unlink系统调用
int rename(const char *oldpath, const char *newpath);
文件重命名

九、symlink/readlink

int symlink(const char *oldpath, const char *newpath);
功能:创建软链接(目录文件只能创建软链接)
oldpath:链接目标
newpath:链接文件
ssize_t readlink(const char *path, char *buf, size_t bufsiz);
功能:读取软链接文件的内容而非链接目标(open打开软链接文件是打开的是目标)
path:链接文件的路径
buf:读取数据的存储位置
bufsize:读取多少个字节

十、mkdir/rmdir

int mkdir(const char *pathname, mode_t mode);
功能:创建目录,目录一定要有执行权限,否则无法打开
成功返回0,失败返回-1
int rmdir(const char *pathname);
功能:删除空目录(只能删除空目录)

十一、chdir/fchdir/getcwd

char* getcwd(char* buf, size_t size);
获取当前进程的工作目录,工作目录是指当不加路径信息时创建/打开时从那个目录下开始找,工作目录默认是程序所在目录
int chdir(const char*path);
功能:修改进程的工作目录
int fchdir(int fd);
功能:被open打开的目录文件的fd

十二、opendir/fdopendir/closedir/readdir/rewinddir/telldir/seekdir

DIR* opendir(const char name)
功能:打开一个目录流(链表)
DIR
fopendir(int fd)
功能:使用文件描述符获取目录流,fd必须是目录文件
readdir(DIR dirp);
struct dirent {
ino_t d_ino; /
inode number i节点号
/
off_t d_off; /* offset to the next dirent 下一个文件节点信息的偏移量*/
unsigned short d_reclen; /* length of this record 当文件节点信息的长度*/
unsigned char d_type; /* type of file; not supported文件类型
by all file system types /
char d_name[256]; /
filename 文件名*/
};
DT_BLK This is a block device.
DT_CHR This is a character device.
DT_DIR This is a directory
DT_FIFO This is a named pipe (FIFO).
DT_LNK This is a symbolic link.
DT_REG This is a regular file.
DT_SOCK This is a UNIX domain socket.
DT_UNKNOWN The file type is unknown.

void rewinddir(DIR* dirp)
功能:把目录流的位置指针调整到开头
long telldir(DIR* dirp)
功能:获取当前目录流的位置在第几个文件节点
void seekdir(DIR* dirp,long offset)
功能:调整当前目录流的位置指针
offset:根据开头指针进行偏移
int closedir(DIR* dirp)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值