Unix file type
   HOME

TheInfoList



OR:

The seven standard Unix file types are ''regular'', ''directory'', ''symbolic link'', ''FIFO special'', ''block special'', ''character special'', and ''socket'' as defined by
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming in ...
. Different OS-specific implementations allow more types than what POSIX requires (e.g. Solaris
doors A door is a hinged or otherwise movable barrier that allows ingress (entry) into and egress (exit) from an enclosure. The created opening in the wall is a ''doorway'' or ''portal''. A door's essential and primary purpose is to provide security by ...
). 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 Most file systems include attributes of files and directories that control the ability of users to read, change, navigate, and execute the contents of the file system. In some cases, menu options or functions may be made visible or hidden dependi ...
field. For regular files,
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, ...
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.


Representations


Numeric

In the stat structure, file type and permissions (the mode) are stored together in a
bit field A bit field is a data structure that consists of one or more adjacent bits which have been allocated for specific purposes, so that any single bit or group of bits within the structure can be set or inspected. A bit field is most commonly used to ...
, which has a size of at least 12 bits (3 bits to specify the type among the seven possible types of files; 9 bits for permissions). The layout for permissions is defined by POSIX to be at the least-significant 9 bits, but the rest is undefined. By convention, the mode is a 16-bit value written out as a six-digit octal number without a leading zero. The format part occupies the lead 4-bits (2 octal digits), and "010" ( in binary) usually stands for a regular file. The next 3 bits (1 digit) are usually used for setuid, setgid, and sticky. The last part is already defined by POSIX to contain the permission. An example is "100644" for a typical file. This format can be seen in
git Git () is a distributed version control system: tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data in ...
,
tar Tar is a dark brown or black viscous liquid of hydrocarbons and free carbon, obtained from a wide variety of organic materials through destructive distillation. Tar can be produced from coal, wood, petroleum, or peat. "a dark brown or black bi ...
, and ar, among other places. The type of a file can be tested using macros like S_ISDIR. Such a check is usually performed by masking the mode with S_IFMT (often the octal number "170000" for the lead 4 bits convention) and checking whether the result matches S_IFDIR. S_IFMT is not a core POSIX concept, but a X/Open System Interfaces (XSI) extension; systems conforming to ''only'' POSIX may use some other methods.


Mode string

Take for example one line in the ls -l output: drwxr-xr-x 2 root root 0 Jan 1 1970 home
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming in ...
specifies 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 Most file systems include attributes of files and directories that control the ability of users to read, change, navigate, and execute the contents of the file system. In some cases, menu options or functions may be made visible or hidden dependin ...
. Therefore, in the example, the mode string is drwxr-xr-x: the file type is d (directory) and the permissions are rwxr-xr-x.


Examples of implementations

The
GNU coreutils The GNU Core Utilities or coreutils is a package of GNU software containing implementations for many of the basic tools, such as cat, ls, and rm, which are used on Unix-like operating systems. In September 2002, the ''GNU coreutils'' were c ...
version of ls uses a call to filemode(), a
glibc The GNU C Library, commonly known as glibc, is the GNU Project's implementation of the C standard library. Despite its name, it now also directly supports C++ (and, indirectly, other programming languages). It was started in the 1980s ...
function (exposed in the
gnulib Gnulib, also called the GNU portability library, is a collection of software subroutines which are designed to be usable on many operating systems. The goal of the project is to make it easy for free software authors to make their software ru ...
library) to get the mode string. FreeBSD uses a simpler approach but allows a smaller number of file types.


Regular file

Regular files show up in ls -l with a
hyphen-minus The hyphen-minus is the most commonly used type of hyphen, widely used in digital documents. It is the only character that looks like a minus sign or a dash in many character sets such as ASCII or on most keyboards, so it is also used as such. ...
- 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) ...


Symbolic link

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 In computer science, inter-process communication or interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow the processes to manage shared data. Typically, applications can use IPC, categoriz ...
. 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 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). The concept is also found in OS ...
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 In computer science, inter-process communication or interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow the processes to manage shared data. Typically, applications can use IPC, categoriz ...
, which enables communication between two processes. In addition to sending data, processes can send
file descriptor In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier ( handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically ha ...
s 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 partition Disk partitioning or disk slicing is the creation of one or more regions on secondary storage, so that each region can be managed separately. These regions are called partitions. It is typically the first step of preparing a newly installed disk ...
s 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 In Unix-like operating systems, a device file or special file is an interface to a device driver that appears in a file system as if it were an ordinary file. There are also special files in DOS, OS/2, and Windows. These special files allow a ...


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

*
file (command) The file command is a standard program of Unix and Unix-like operating systems for recognizing the type of data contained in a computer file. History The original version of file originated in Unix Research Version 4 in 1973. System V brought ...


References

{{DEFAULTSORT:Unix File Types Unix file system technology