Unix file types

The standard Unix file types are regular, directory, symbolic link, FIFO special, block special, character special, and socket as defined by POSIX.[1] Different OS-specific implementations allow more types than what POSIX requires (e.g. Solaris doors). A file's type can be identified by the ls -l command, which displays the type in the first character of the file system permissions field.

For regular files, Unix does not impose or provide any internal file structure; therefore, their structure and interpretation is entirely dependent on the software using them. However, the file command can be used to determine what type of data they contain.

Mode string

Take for example one line in the ls -l output:

drwxr-xr-x 2 root root     0 Jan  1  1970 home

POSIX specifies[2] the format of the output for the long format (-l option). In particular, the first field (before the first space) is dubbed the "file mode string" and its first character describes the file type. The rest of this string indicates the file permissions.

Therefore, in the example, the mode string is drwxr-xr-x: the file type is d (directory) and the permissions are rwxr-xr-x.

Internally, ls obtains the stat structure[3] associated with the file and transforms the mode_t field into a human-readable format. Note that mode_t is actually a bit field with two parts; the file type is stored within the S_IFMT mask. It can be tested with some macros like S_ISDIR (for the S_IFDIR value with mask S_IFMT) to get the file type flags.

Examples of implementations

The GNU coreutils version of ls uses a call to filemode(), a glibc function (exposed in the gnulib library[4]) to get the mode string.

FreeBSD uses a simpler approach[5] but allows a smaller number of file types.

Regular file

Regular files show up in ls -l with a hyphen-minus - in the mode field:

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

Directory

The most common special file is the directory. The layout of a directory file is defined by the filesystem used. As several filesystems are available under Unix, both native and non-native, there is no one directory file layout.

A directory is marked with a d as the first letter in the mode field in the output of ls -dl or stat, e.g.

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

$ stat /
  File: "/"
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 802h/2050d      Inode: 128         Links: 26
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
...

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).

A symbolic link is marked with an l (lower case L) as the first letter of the mode string, e.g.

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

FIFO (named pipe)

One of the strengths of Unix has always been inter-process communication. Among the facilities provided by the OS are pipes, which connect the output of one process to the input of another. This is fine if both processes exist in the same parent process space, started by the same user, but there are circumstances where the communicating processes must use FIFOs, here referred to as named pipes. One such circumstance occurs when the processes must be executed under different user names and permissions.

Named pipes are special files that can exist anywhere in the file system. They can be created with the command mkfifo as in mkfifo mypipe.

A named pipe is marked with a p as the first letter of the mode string, e.g.

prw-rw---- ... mypipe

Socket

A socket is a special file used for inter-process communication, which enables 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 which allow only unidirectional data flow, sockets are fully duplex-capable.

A socket is marked with an s as the first letter of the mode string, e.g.

srwxrwxrwx /tmp/.X11-unix/X0

Device file (block, character)

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 is network devices, which do not turn up in the file system but are handled separately.

Device files are used to apply access rights to the devices 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 accept a serial stream of output
  • Block devices are randomly accessible

Although, for example, disk partitions may have both character devices that provide un-buffered random access to blocks on the partition and block devices that provide buffered random access to blocks on the partition.

A character device is marked with a c as the first letter of the mode string. Likewise, a block device is marked with a b, e.g.

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

Door

A door is a special file for inter-process communication between a client and server, currently implemented only in Solaris.

A door is marked with a D (upper case) as the first letter of the mode string, e.g.

Dr--r--r-- ... name_service_door

See also

References

  1. "<sys/stat.h>". The Open Group Base Specifications Issue 6. The Open Group. 21 July 2019.
  2. "IEEE Std 1003.1-2008 ls". The Open Group. 11 March 2017.
  3. "IEEE Std 1003.1-2008 <sys/stat.h>". The Open Group. 11 March 2017.
  4. "filemode function in GNU coreutils". GNU. 11 March 2017.
  5. "printtype function from FreeBSD". FreeBSD. 11 March 2017.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.