In this section, we shall discuss some of the most commonly used syscalls for file operations. These include
stat: get file status information
int stat(char *filename, struct stat *buf)
int fstat(int filedes, struct stat *buf)
int lstat(char *filename, struct stat *buf)
open : open a file for READ, WRITE, APPEND
int open(char *file, int flags, int mode)
close : close an opened file descriptor
int close(int fd)
read : read from an opened file descriptor
int read(int fd, char buf[ ], int count)
write : write to an opened file descriptor
int write(int fd, char buf[ ], int count)
lseek : reposition R|W offset of a file descriptor
int lseek(int fd, int offset, int whence)
dup : duplicate file descriptor into the lowest available descriptor number
int dup(int oldfd);
dup2 : duplicate oldfd into newfd; close newfd first if it was open
int dup2(int oldfd, int newfd)
link : hard link newfile to oldfile
int link(char *oldPath, char *newPath)
unlink : unlink a file; delete file if file’s link count reaches 0
int unlink(char *pathname);
symlink : create a symbolic link
int symlink(char *target, char *newpath)
readlink: read contents of a symbolic link file
int readlink(char *path, char *buf, int bufsize)
umask : set file creation mask; file permissions will be (mask & ~umask)
int umask(int umask);
Source: Wang K.C. (2018), Systems Programming in Unix/Linux, Springer; 1st ed. 2018 edition.