Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to judge whether a folder or directory exists under Linux

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to determine the existence of a folder or directory under Linux", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn "how to determine the existence of a folder or directory under Linux".

Tat series functions

The stat function is used to return structural information related to the file. There are three cases of stat series functions, corresponding to file names, file descriptors, and symbolic link files. The stat structure describes the attributes of a file, including the file type, file size, and so on. The detailed stat structure is as follows:

1 struct stat {2 mode_t st_mode; / / file type & mode (permissions) 3 ino_t st_ino; / / i-node number (serial number) 4 dev_t st_dev; / / device number (filesystem) 5 dev_t st_rdev; / / device number for specials files6 nlink_t st_nlink; / / number of links7 uid_t st_uid / / user ID of owner8 gid_t st_gid; / / group ID of owner9 off_t st_size; / / size in bytes, for regular files10 time_t st_atime; / / time of last access11 time_t st_mtime; / / time of last modification12 time_t st_ctime; / / time of last file status change13 long st_blksize / / best I/O block size14 long st_blocks; / / number of 512-byte blocks allocated15}

We can get information such as file type and file size through stat. File types are: normal files, directory files, block special files, character special files, FIFO, sockets and symbolic links. If you want to determine whether a file or directory exists through stat series functions, when you execute the stat function, if the file exists, you need to further determine whether the file is a normal file or a directory file.

An error of stat series function returned-1. The error code is stored in errno. The value of errno is as follows:

1. The file specified by the ENOENT parameter file_name does not exist. 2. The directory in the ENOTDIR path exists but is not a real directory. 3. The file to be opened by ELOOP has too many symbolic connection problems, the upper limit is 16 symbolic connection 4, the EFAULT parameter buf is an invalid pointer, pointing to the non-existent memory space 5, the EACCESS access file is rejected 6, the ENOMEM core memory is insufficient 7, the path name of the ENAMETOOLONG parameter file_name is too long access function

The access function tests the access rights according to the actual user ID and the actual group. The function prototype is:

# include int access (const char * pathname, int mode)

Value of mode:

F_OK test whether the file exists

R_OK test read permission

W_OK test write permissions

X_OK test execution permissions

To correctly judge the existence of a file is to use the access function, which is implemented as follows:

Oepndir function

The opendir function is used to open the file directory and return a pointer successfully or NULL if an error occurs. The implementation is as follows:

The above is all the contents of the article "how to determine whether a folder or directory exists under Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report