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 '-
'.
Take for example one line in the ls -l
output:
POSIX specifies the format of the output of the long format (-l
option) for every file listed. In particular, the first field (before first space) is dubbed "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, what ls
does is obtaining the stat
structure associated with the file and transforming the mode_t
field into a human-readable format. Note that mode_t
is actually a bit field with two parts: in mask S_IFMT
the file type is stored. 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.
Different OS-specific implementations allow more types than what POSIX requires (e.g. Solaris doors).
The GNU coreutils version of ls
uses a call to filemode()
, a glibc function (exposed in the gnulib library) to get the mode string.
FreeBSD uses a simpler approach but allows a smaller amount of file types.
Files are also called "regular files" to distinguish them from "special files". They show up in ls -l
without a specific character in the mode field: