Unix 文件類型

Unix File Types (Unix 文件類型)

For normal files in the file system, Unix does not impose or provide any internal file structure. This implies that from the point of view of the operating system, there is only one file type.

The structure and interpretation thereof is entirely dependent on how the file is interpreted by software.

Unix does however have some special files. These special files can be identified by the ls -l command which displays the type of the file in the first alphabetic letter of the file system permissions field. A normal (regular) file is indicated by a hyphen-minus '-'.

而對於一些比較特殊的文件,Unix系統裡則用一些比較比較特殊的系統權限(permissions mode的第一個字母)來區分,讓人們更了解文件與系統的關係。
一般常規的文件permissions mode的第一個字母為'-',下面我們將來為大家解說關於不同類型的Unix文件類型。

Regular file (一般常規文件)

$ ls -dl /etc/passwd
-rw-r--r-- ... /etc/passwd

Directory (文件夾)

$ ls -dl /
drwxr-xr-x 26 root root 4096 Sep 22 09:29 /

Symbolic link (軟鏈接=win中的快捷方式)
A symbolic link is a reference to another file. This special file is stored as a textual representation of the referenced file's path (which means the destination may be a relative path, or may not exist at all).

lrwxrwxrwx ... termcap -> /usr/share/misc/termcap
lrwxrwxrwx ... S03xinetd -> ../init.d/xinetd

Named pipe (管道)
In computing, a named pipe (also known as a FIFO for its behavior) is an extension to the traditional pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication (IPC).
named pipe是一種被命名了的單向管道(先進先出的簡單結構),利用mkfifo來進行創建,可以讓兩個程式間進行簡單的溝通。範例如下:

    mkfifo my_pipe
    gzip -9 -c < my_pipe > out.gz &
    cat file > my_pipe
    rm my_pipe

prw-rw---- ... mypipe

A socket is a special file used for inter-process communication. These allow communication between two processes. In addition to sending data, processes can send file descriptors across a Unix domain socket connection using the sendmsg() and recvmsg() system calls.
Unlike named pipes sockets are fully duplex-capable.
有別於named pipes,socket是雙向的,也是用在兩個或多個程序間的溝通,用法與網絡中使用的socket相當類似。

srwxrwxrwx ... /tmp/.X11-unix/X0

Device file
In Unix, almost all things are handled as files and have a location in the file system—even hardware devices like hard drives. The great exception for devices and the files that represent them are network devices that do not turn up in the file system but are handled separately.
Device files are used to apply access rights and to direct operations on the files to the appropriate device drivers. Unix makes a distinction between character devices and block devices. The distinction is roughly as follows:

character devices provide only a serial stream of input or output;
block devices are randomly accessible;

character devices(字符设备)的內容沒有的特殊結構,就只有簡單的字串流,而block devices的讀取方式是以block為單位的,可以做到隨機讀取的效果,比如硬盤就屬於block devices,serial ports屬於character devices。

crw------- ... /dev/null
brw-rw---- ... /dev/sda

Reference Links:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.