Home  >  Article  >  System Tutorial  >  Explore file types in Linux systems: ordinary files, directories, and special files

Explore file types in Linux systems: ordinary files, directories, and special files

王林
王林Original
2024-06-27 10:58:25727browse

探索 Linux 系统中的文件类型:普通文件、目录与特殊文件

The following sections discuss the basic functions of performing I/O when developing C language in Linux. The Linux system has several file types. However, in the process of giving examples and experiments, there are several file types in the Linux system, all of which revolve around ordinary files.

linux系统有几种文件类型_linux典型文件系统类型_linux特有的文件系统File types in linux

Remember in Section 9, we talked about how Unix systems (Linux is a Unix-like system) think "everything is a file"? Most files in unix systems are ordinary files and directories, and these two types of files are also the most commonly used. For example, the /usr directory and the hello.txt text file above it belong to ordinary file types.

In fact, linux site:infoq.cn, the linux system divides all files into the following categories:

It seems that the devices in the Linux system (such as hard disk, parallel port, etc.) are either block special files or character special files.

Get file types in linux

Linux provides the stat series of functions to obtain statistical information of files. Enter manstat in Linux to get the usage guide of the stat function:

linux系统有几种文件类型_linux典型文件系统类型_linux特有的文件系统

linux系统有几种文件类型_linux特有的文件系统_linux典型文件系统类型The second parameter of the stat function is a structure, its definition can be found in:

 struct stat {
 unsigned long st_dev;
 unsigned long st_ino;
 unsigned short st_mode;
 unsigned short st_nlink;
 unsigned short st_uid;
 unsigned short st_gid;
 unsigned long st_rdev;
 unsigned long st_size;
 unsigned long st_blksize;
 unsigned long st_blocks;
 unsigned long st_atime;
 unsigned long st_atime_nsec;
 unsigned long st_mtime;
 unsigned long st_mtime_nsec;
 unsigned long st_ctime;
 unsigned long st_ctime_nsec;
 unsigned long __unused4;
 unsigned long __unused5;
};	

linux特有的文件系统_linux系统有几种文件类型_linux典型文件系统类型

linux典型文件系统类型_linux系统有几种文件类型_linux特有的文件系统It can be seen that only the statistical function stat can obtain various information about the file, such as user group ID, user ID, and file size. The st_mode member records the file type and mode (permissions). Use the following macros to get the file type:

...
#define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
#define S_ISCHR(mode) __S_ISTYPE((mode), __S_IFCHR)
#define S_ISBLK(mode) __S_ISTYPE((mode), __S_IFBLK)
#define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
#ifdef __S_IFIFO
# define S_ISFIFO(mode) __S_ISTYPE((mode), __S_IFIFO)
#endif
#ifdef __S_IFLNK
# define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
#endif
...

linux特有的文件系统_linux系统有几种文件类型_linux典型文件系统类型C language example, get linux file type

晓得了stat函数和以上几个宏,编撰C语言程序获取文件的类型是便捷的:

#include 
#include 
#include 
#include 
int main(int argc, char* argv[])
{
 if(argc < 2){
 printf("ntusage:");
 printf("ntt%s filepathn", argv[0]);
 return -1;
 }
 struct stat tmp;
 char* res;
 lstat(argv[1], &tmp);
 if(S_ISREG(tmp.st_mode))
 res = "regular";
 else if(S_ISDIR(tmp.st_mode))
 res = "directory";
 else if(S_ISBLK(tmp.st_mode))
 res = "block";
 else if(S_ISLNK(tmp.st_mode))
 res = "link";
 else if(S_ISFIFO(tmp.st_mode))
 res = "fifo";
 else if(S_ISSOCK(tmp.st_mode))
 res = "socket";
 else
 res = "unknown";
 printf("%s : %sn", argv[1], res);
 return 0;
}

以上代码使用了lstat函数,而不是stat函数,它俩的功能是相同的。惟一不同之处在于处理符号链接时,lstat是将该符号链接直接作为文件处理的,而stat函数则是处理该符号链接指向的文件。

linux特有的文件系统_linux系统有几种文件类型_linux典型文件系统类型

编译以上代码,执行之:

# gcc t6.c
# ./a.out 
 usage:
 ./a.out filepath
# ./a.out ../
../ : directory
# ./a.out t.c
t.c : regular
root@lcc:/lccRoot/C/tmp# ./a.out ../
../ : directory
# ./a.out /dev/log
/dev/log : socket
#

linux典型文件系统类型_linux特有的文件系统_linux系统有几种文件类型

程序接收一个参数,并返回该参数的类型。其他几种类型文件的测试留给读者,在这一过程中可以顺便了解一下linux中的文件组成。

欢迎在评论区一起讨论linux文本编辑器,指责。文章都是手打原创,每晚最扼要的介绍C语言、linux等嵌入式开发,喜欢我的文章就关注一波吧,可以见到最新更新和之前的文章哦。

The above is the detailed content of Explore file types in Linux systems: ordinary files, directories, and special files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn