Linux 文件类型

最后更新:
阅读次数:

In linux,everything is a file。

日常生活中,我们经常使用不同的文件后缀来表示不同文件类型,比如:*.jpg 是图片文件,*.md 是 MarkDown 文件等。但是从 Linux 系统层面讲,其实我们日常见到的大多数文件都属于 Linux 的 常规文件普通文件

除过这些常规文件,还有一些特殊的文件类型,比如目录文件、设备文件等。下面我们来一一了解一下这些文件!

常规文件(Regular File)

常规文件,即普通意义上的文件,比如一份 pdf 文档、一张图片、一个视频等。依照文件的内容,常规文件又可以分为纯文本文档(ASCII)二进制文件(binary)数据格式文件(data)等。

目录文件(Directory File)

Linux 中目录也是一种文件。目录文件包括了文件夹中所有文件的名字,以及文件夹自身的 inode 和文件夹中每个文件的 inode

An inode is a data structure on a filesystem that stores all the information about a file except its name and its actual data.

See Directory Definition

设备文件(Device File)

In Unix-like operating systems, a device file or special file is an interface for a device driver that appears in a file system as if it were an ordinary file.

设备文件为周边硬件设备(比如打印机、串行端口等)提供了简单的接口,它可以被这些硬件设备用来获取诸如磁盘分区等特殊的资源。此外,设备文件还可以用来获取那些与系统并没有任何实际的硬件设备连接的系统资源,比如数据接收器、随机数生成器等。

在类 Unix 系统中,有两种类型的设备文件:字符设备文件与块设备文件。这两种设备文件的区别在于系统或硬件如何向它们写入数据与读出数据。与命名管道相比,这两种设备文件可以统称为设备特殊文件,它们都不是普通文件,并且也不会与硬件设备进行直接连接。

当向一个设备文件进行读数据或写数据操作时,发起这个操作的请求会被相关设备的驱动程序处理。每个设备文件都有一个与之关联的数字,而这个数字会被驱动程序使用。

linux-device

字符设备文件(Character Device)

Character special files or character devices provide unbuffered, direct access to the hardware device. They do not necessarily allow programs to read or write single characters at a time; that is up to the device in question. The character device for a hard disk, for example, will normally require that all reads and writes are aligned to block boundaries and most certainly will not allow reading a single byte.

Character devices are sometimes known as raw devices to avoid the confusion surrounding the fact that a character device for a piece of block-based hardware will typically require programs to read and write aligned blocks.

Character devices files behave like pipes, serial ports, etc. Writing or reading to them is an immediate action. What the driver does with the data is its own business. Writing a byte to a character device might cause it to be displayed on screen, output on a serial port, converted into a sound, … Reading a byte from a device might cause the serial port to wait for input, might return a random byte (/dev/urandom), … The name “character device” comes from the fact that each character is handled individually.

块设备文件(Block Device)

Block special files or block devices provide buffered access to hardware devices, and provide some abstraction from their specifics. Unlike character devices, block devices will always allow the programmer to read or write a block of any size (including single characters/bytes) and any alignment. The downside is that because block devices are buffered, the programmer does not know how long it will take before written data is passed from the kernel’s buffers to the actual device, or indeed in what order two separate writes will arrive at the physical device; additionally, if the same hardware exposes both character and block devices, there is a risk of data corruption due to clients using the character device being unaware of changes made in the buffers of the block device.

Most systems create both block and character devices to represent hardware like hard disks. FreeBSD and Linux notably do not; the former has removed support for block devices, while the latter creates only block devices. In Linux, to get a character device for a disk one must use the “raw” driver, though one can get the same effect as opening a character device by opening the block device with the Linux-specific O_DIRECT flag.

Block devices (also called block special files) usually behave a lot like ordinary files: they are an array of bytes, and the value that is read at a given location is the value that was last written there. Data from block device can be cached in memory and read back from cache; writes can be buffered. Block devices are normally seekable (i.e. there is a notion of position inside the file which the application can change). The name “block device” comes from the fact that the corresponding hardware typically reads and writes a whole block at a time (e.g. a sector on a hard disk).

命名管道(named pipe 或 FIFO)

命名管道(FIFO, 也称 named pipe),是一种特殊的文件,进程间互相通信的方式。与匿名管道不同,该管道将具象化为一个具体的文件,而且可以在不相关的进程间建立通信关系,效果就如同匿名管道的那样。

套接字文件(local socket file)

Sockets are a special file type, similar to TCP/IP sockets, providing inter-process networking protected by the file system’s access control.

# open a listening socket in one terminal
$ nc -lU socket.sock

# send data from another terminal:
$ echo mytext | nc -U socket.sock

符号链接(symbolic link)

此种文件仅是一个指向其它文件的链接,可以理解为 Windows 系统上的快捷方式!

符号链接,也称软链接,相对应的还有硬链接,两者的区别请查看我的文章【Linux 文件属性】

$ echo 'some text' > file.txt

$ ln -s file.txt softlink.txt

$ cat softlink.txt
some text

$ ls -ld softlink.txt
lrwxrwxrwx 1 lubos lubos 5 Jan 10 14:42 softlink.txt -> file.txt

参考资料