一切皆文件
在 Linux 下一切皆是文件, 从应用层 (应用程序层) 看待底层机制时, 皆以文件的方式来看待这些机制. 但是不同的底层机制, 毕竟是不同的, 比如底层驱动程序, 就分为字符设备驱动和块设备驱动, 根据底层机制的不同, 文件被分为了 7 种类型.
文件的 7 种类型
文件一共分为 7 类分别是 - d c s p l b
注意: ls -F 也可以查看文件, 但这里的分类不全按照 7 种类型划分
以 * 结尾: 可执行文件
以 @结尾: 软连接
以 / 结尾: 目录
其他: 普通文件
- [root@51cto 1360]# ls -F /dev/
- agpgart hugepages/ mem ram3 sg0 tty18 tty36 tty54 vcs
- block/ hvc0 net/ ram4 sg1 tty19 tty37 tty55 vcs1
- bsg/ input/ network_latency ram5 shm/ tty2 tty38 tty56 vcs2
- cdrom@ kmsg network_throughput ram6 snapshot tty20 tty39 tty57 vcs3
- cdrw@ log= null ram7 sr0 tty21 tty4 tty58 vcs4
- char/ loop0 nvram ram8 stderr@ tty22 tty40 tty59 vcs5
- console loop1 oldmem ram9 stdin@ tty23 tty41 tty6 vcs6
- core@ loop2 port random stdout@ tty24 tty42 tty60 vcsa
- cpu/ loop3 ppp raw/ systty@ tty25 tty43 tty61 vcsa1
- cpu_dma_latency loop4 ptmx root@ tty tty26 tty44 tty62 vcsa2
- crash loop5 pts/ rtc@ tty0 tty27 tty45 tty63 vcsa3
- disk/ loop6 ram0 rtc0 tty1 tty28 tty46 tty7 vcsa4
- dvd@ loop7 ram1 scd0@ tty10 tty29 tty47 tty8 vcsa5
- dvdrw@ lp0 ram10 sda tty11 tty3 tty48 tty9 vcsa6
- fb@ lp1 ram11 sda1 tty12 tty30 tty49 ttyS0 vga_arbiter
- fb0 lp2 ram12 sda2 tty13 tty31 tty5 ttyS1 zero
- fd@ lp3 ram13 sda3 tty14 tty32 tty50 ttyS2
- full MAKEDEV@ ram14 sda4 tty15 tty33 tty51 ttyS3
- fuse mapper/ ram15 sda5 tty16 tty34 tty52 urandom
- hpet mcelog ram2 sda6 tty17 tty35 tty53 usbmon0
- View Code
普通文件(regular file:-)
普通文件根据存放的内容的不同, 又分为如下两种
文本文件
存放的都是文字编码, 文本编辑器打开后, 会将这些文字编码翻译为文字图形, 以供人识别. 这种文件打开后你能看懂
纯二进制文件(机器码)
比如经过编译后得到的可执行文件, 里面放的是 cpu 执行的纯二进制机器码, 由于文编编辑器只认识文字编码, 所以用文本编辑器打开后, 显示的内容无法是错乱的, 无法辨识. 这种文件打开后你看不懂.
但是, 无论还是在计算机中存储时, 其实都是以二进制形式存放的, 因为计算机只认识 0,1. 对 linux 内核而言, 这两种文件并无区别, 至于文件中的数据如何解释, 则由处理这些数据的应用程序 (比如文本编辑器) 来决定.
Linux 针对文件操作提供的 API 函数, 如 read,write 等, 在使用这两种形式的文件时没有任何区别.
目录文件(director file:d)
目录是一种特殊的文件, 专门用于管理其它文件.
参考: Linux 磁盘管理 -- 文件系统 https://www.cnblogs.com/kelamoyujuzhen/p/9154643.html
字符设备文件(character special file:c)
典型设备: 打印机
字符设备文件, 就是字符设备驱动程序在上层的表现形式. 当应用程序调用底层字符设备驱动程序, 实现对某个字符设备进行读写时, 上层就需要对接底层的字符驱动程序. 类似于抗日剧里面, 汉奸是日本人剥削老百姓的代言人一样, 字符设备文件是底层字符设备驱动程序的代言人. 我们通过 open,read,write 去读写字符设备文件, 就实现了和底层字符设备驱动的交互.
块设备文件(block special file:b)
来源: http://www.bubuko.com/infodetail-2704142.html