Directory operations in Linux <1>

Source: Internet
Author: User

I. Permissions for the directory

(1) The directory file access rights are divided into three groups, respectively, the owner, the user, and others. There are 3 permission bits for each permission group, read, write, and execute, respectively.


Note: You can use the STAT function to get the status information for a directory file. The permissions are st_mode in the stat structure.

(2) Test Directory access permissions: The program gets the directory file status information, if it is a non-directory file, then the program exits. The program checks whether the owner user of the catalog file has read-write and point-to permissions and outputs the results in full.

1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4#include <sys/stat.h>5 intMainvoid)6 {7     structStat buf;/*Store file status information*/8     if(Stat (" /Home", &buf) = =-1){/*Get file status information*/9Perror ("fail to stat");TenExit1); One     } A     if(! S_isdir (Buf.st_mode)) {/*Non-catalog files*/ -printf"This is not a directory file\n"); -Exit1); the     } -     if(S_irusr & Buf.st_mode)/*owner user has read directory permissions*/ -printf"user can read the dir\n"); -     if(S_iwusr & Buf.st_mode)/*owner user has write directory permission*/ +printf"user can write the dir\n"); -     if(S_ixusr & Buf.st_mode)/*owner user has execute directory permission*/ +printf"user can through the dir\n"); A     return 0; at}


Two Create a directory

(1) function

mkdir (const char* pathname,mode_t mode);

(2) return

Success: 0


(3) Implement Create directory

1#include <stdio.h>2#include <unistd.h>3#include <stdlib.h>4#include <sys/stat.h>5 intMainvoid)6 {7     if(MkDir ("/home/tmp", S_IRUSR | S_IWUSR | S_IXUSR) = =-1){/*Chong8 Build a directory*/9Perror ("fail to mkdir");TenExit1); One     } Aprintf"successfully make a dir\n");/*Output hint Information*/ -     return 0; -}


Third, delete a directory

(1) function: int rmdir (const char*pathname)

return value:

Success: 1


(2) Realize

1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4 intMainvoid)5 {6     if(RmDir ("/home/tmp") == -1){/*Output a directory*/7Perror ("fail to Rmkdir");8Exit1);9     }Tenprintf"successfully remove a dir\n");/*Output hint Information*/ One     return 0; A}


Directory operations in Linux <1>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.