1. Get the path of the current directory: getwd() getcwd() get \ current \ dir \ name()
2. Reassign the process to a new directory, that is, set the current path: chdir()/fchdir()
3. Create directory: mkdir() delete directory rmdir()
4. Open a directory: opendir() close a directory: closedir()
5. Read the contents of the directory: readdir(), which returns an array containing the node number and name of the directory. See the following example to list all the file names under the directory
6. Creation of special files: mknod (pipeline and equipment files) unwritten code test
7. Mount and unmount of files: mount() unmount() (root user required) unwritten code test
8. Hard link: link() soft link: symlink() read the content of the soft link itself: readlink()
/* get_current_dir_name()Use the macro that needs to be defined and placed before all header files */ #define _GNU_SOURCE #include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <dirent.h> int main(void) { char buf[100] = {0}; char buf_read[100] ={0}; struct dirent *dirent1; getcwd(buf,100); //Method 1: getcwd() gets the path of the current directory printf("%s\n",buf); printf("%s\n",get_current_dir_name()); //Method 2: get "current" dir "name() to get the path of the current directory if (mkdir("./test",0777) == -1) //mkdir() creates a directory test and deletes the non empty directory rmdir() { perror("mkdir"); } if (chdir("./test") == -1) //chdir () redirects the current process to the test directory without affecting the parent process { perror("chdir"); } printf("current dir is: %s\n",get_current_dir_name()); //The current directory becomes test DIR * dp = opendir(get_current_dir_name()); //opendir() opens a directory while ((dirent1 = readdir(dp)) != NULL) //readdir() loop output the contents of the file directory printf("read dirent d_name %s\n",dirent1->d_name); if (link("./text.txt", "./text1.txt") == -1) //link(), hard link, equivalent to backup { perror("link"); } if (symlink("./text.txt", "./text2") == -1) //symlink() soft link, creating a shortcut { perror("symlink"); } int fd = open("./text2"); //Open soft link file read(fd, buf_read, 100); //Read () is used to read the contents of the file it points to printf("text2 content is: %s\n",buf_read); close(fd); readlink("./text2", buf_read,100); //readlink () reads the contents of the soft link itself, that is, the name of the linked file printf("text2 real content is: %s\n",buf_read); closedir(dp); return 0; }
Operation result: