Jump to content

Unix file types

From Wikipedia, the free encyclopedia

The Unix file types are the categories of file formats that a Unix-based system uses to provide context-sensitive behavior of file system items – all of which called files in Unix-based systems. POSIX defines categories: regular, directory, symbolic link, FIFO special, block special, character special, and socket.[1] An operating system may define additional categories (e.g. Solaris doors).

A regular file is any file format that the file system does not know and relies on applications to manipulate.[2] The other categories are for file formats that the file system inherently knows and can manipulate.

The ls -l command reports a file's category via the character before the permissions information. The file command reports file format information; even for regular files.[3]

Representations

[edit]

Numeric

[edit]

The stat() system call reports Unix file type of a st_mode (mode) bit field of a struct stat parameter. Generally, the mode field is 16 bits wide with the Unix file type represented in the first 4 bits and the rest allocated for file permissions. POSIX defines the 9 least significant bits for access permissions; leaving the rest as implementation detail.[1]

When written as octal, a mode value shows the Unix file type separately – as the first two digits. For example, mode of octal 100644 indicates a regular file since the Unix file type bit-field is octal 10. This format is used in git, tar, ar, and other contexts.[4]

A program can test a mode value to determine Unix file type via macros provided in standard C headers. For example, a program can mask a mode value with S_IFMT (octal 170000 for the first 4 bits convention) to obtain the Unix file type and then test that value against S_IFDIR to determine whether the file is a directory. Alternatively, a program can use the S_ISDIR macro. 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.[1]

Symbolic

[edit]

POSIX specifies the long format of the ls command to represent the Unix file type as the first letter for an entry.[5]

Unix file type indicators
type symbol
regular -
directory d
symbolic link l
FIFO special p
block special b
character special c
socket s

The GNU coreutils version of ls calls the gnulib function filemode()[6] to format the mode string. FreeBSD uses a simpler approach but allows a smaller number of file types.[7]

Examples

[edit]

Command ls -l reports the file mode string as the first column of output for each file. This string starts with the Unix file type indicator. The following is output for the root directory from command ls -l / which starts with d to indicate that it's a directory. The rest of the first column rwxr-xr-x indicates file permissions.

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

Output from command stat / includes the full Unix file type name:

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

Output for a symbolic link file starts with an initial l (lower case 'L') and identifies the referenced file path as in this example ls -l output:[5]

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

A named pipe is a special file that can be created via the command mkfifo name. A named pipe is identified as p as in this example ls -l output:[5]

prw-rw---- ... mypipe

A socket is a special file for inter-process communication that unlike named pipes allows for full duplex. A socket is marked with s as in this example ls -l output:[5]

srwxrwxrwx /tmp/.X11-unix/X0

Block and character files represent hardware devices. A device file can be used to control access to a device and to allow file-like operations on the connected device. A character device (serial access) is marked with a c and a block device (random access) is marked with a b as in this example ls -l output:[5]

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

References

[edit]
  1. ^ a b c "<sys/stat.h>". The Open Group Base Specifications Issue 6. The Open Group. 21 July 2019. Archived from the original on 27 November 2016. Retrieved 10 February 2017.
  2. ^ Loukides, Mike (October 2002). "When Is a File Not a File?". Unix Power Tools (3 ed.). O'Reilly. p. 80. ISBN 9780596003302. A file is nothing more than a stream of bytes ...
  3. ^ "file". IEEE Std 1003.1-2017 (POSIX). The Open Group. 2018. Archived from the original on 2018-10-12. Retrieved 2023-10-26.
  4. ^ Kitt, Stephen. "What file mode is a symlink?". Unix & Linux Stack Exchange.
  5. ^ a b c d e "ls". IEEE Std 1003.1-2008 (POSIX). The Open Group. 11 March 2017. Archived from the original on 3 August 2017. Retrieved 10 February 2017.
  6. ^ "filemode function in GNU coreutils". GNU. 11 March 2017.
  7. ^ "printtype function from FreeBSD". FreeBSD. 11 March 2017.