Linux下查看一个文件的类型
2013-11-13 20:23
471 查看
Linux操作系统中,文件类型分为七类:
1、普通文件
2、目录(文件夹)
3、面向块的设备文件(磁盘、磁带)
4、面向字符的设备文件(纸带输入/穿孔输入机、打印机)
5、符号链接
6、管道pipe及命名管道FIFO
7、套接字
下面是使用stat系统调用函数来判断文件类型的自定义函数:
以上代码中,通过以st_mode作为参数来检查文件类型的宏包括:
其中,参数m代表st_mode。
1、普通文件
2、目录(文件夹)
3、面向块的设备文件(磁盘、磁带)
4、面向字符的设备文件(纸带输入/穿孔输入机、打印机)
5、符号链接
6、管道pipe及命名管道FIFO
7、套接字
下面是使用stat系统调用函数来判断文件类型的自定义函数:
#include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<stdio.h> void file_type(char* path) { struct stat stat_buf; int res=stat(path,&stat_buf); if(res==0) { if(S_ISREG(stat_buf.st_mode)) printf("%s is regular file\n",path); else if(S_ISDIR(stat_buf.st_mode)) printf("%s is directory\n",path); else if(S_ISCHR(stat_buf.st_mode)) printf("%s is character device\n",path); else if(S_ISBLK(stat_buf.st_mode)) printf("%s is block device\n",path); else if(S_ISSOCK(stat_buf.st_mode)) printf("%s is socket\n",path); else if(S_ISLNK(stat_buf.st_mode)) printf("%s is soft link\n",path); else if(S_ISFIFO(stat_buf.st_mode)) printf("%s is FIFO\n",path); } else printf("path error\n"); } int main(int* argc,char* argv[]) { char path[50]; printf("Please input the file path:\n"); while(scanf("%s",path)!=EOF) file_type(path); return 0; }
以上代码中,通过以st_mode作为参数来检查文件类型的宏包括:
S_ISREG(m) is it a regular file? S_ISDIR(m) directory? S_ISCHR(m) character device? S_ISBLK(m) block device? S_ISFIFO(m) FIFO (named pipe)? S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.) S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
其中,参数m代表st_mode。
相关文章推荐
- 一天一个linux基础命令之查看文件类型file
- 每天一个linux命令(24):Linux文件类型与扩展名
- 【每天一个Linux命令】25. 查看文件命令(cat/more/less/tail/tac/nl/od)
- Linux操作系统的简单指令及如何使用vim编写一个程序,然后使用gcc查看【预处理】、【编译】、【汇编】、【链接】各阶段文件的内容。
- 每天一个linux命令(24):Linux文件类型与扩展名
- LINUX下查看一个文件夹下所有文件的总大小
- 1.2.在linux环境实践,使用vim编写一个程序,然后使用gcc查看【预处理】、【编译】、【汇编】、【链接】各阶段文件的内容。并熟悉相关指令。
- 每日掌握一个Linux命令 之 探测给定文件的类型命令 file
- linux学习入门2——linux文件系統基本结构(linuxcast.net)(倒转树状结构、命名机制、查看文件类型)
- 查看linux设备文件系统类型的几种方法
- Linux查看分区文件系统类型总结
- Linux下如何查看一个文件夹下有多少文件
- Linux 查看文件系统类型
- linux 查看文件系统类型
- linux查看分区文件系统类型
- linux 如何查看文件系统的类型
- Linux下七种文件类型、文件属性及其查看方法
- Linux下查看cpu类型、内存大小、硬盘大小类型和文件系统大小等
- 每天一个linux命令(24)--Linux文件类型与扩展名
- 在Linux云系统上怎么查看某个软件的安装路径和查找一个文件的路径