The type is part of the inode
The mode field of an inode contains a few bits that identify the file type, and the rest of the mode holds the permissions. The kernel checks the type before it applies a system call. Opening a directory for writing as if it were a file fails, and reading a socket with cat does not give you a file. The file type is set when the inode is created and does not change for the life of the inode.
The long listing shows the type as the first character of each line. The most common values are the hyphen for a regular file, the letter d for a directory, and the letter l for a symbolic link. The others are c for a character device, b for a block device, p for a named pipe, and s for a socket. The command stat -c %F prints the same information as words.
stat -c '%F' /etc /dev/null /etc/hostname 2>/dev/null
ls -l /dev/nullThe directory /etc is reported as a directory. The device /dev/null is a character special file. Its listing starts with c, and the two numbers before the date are its major and minor device numbers, which identify the driver and the particular device.
Regular files and directories
A regular file is the type most people mean by a file. Its data is a sequence of bytes, and the filesystem maps those bytes to blocks. Most programs work only with regular files, which is why many tools fail or behave oddly when given a device or a FIFO. A directory is also a file to the kernel, but its contents are records of names and inode numbers, as the earlier unit explained. The kernel allows only a few operations on a directory, such as listing it and looking up names within it.
Symbolic links
A symbolic link is a file whose data is a path. It is listed with the type l, and it can point at any type of inode, including a directory or a device. The kernel follows it during path lookup, so most programs never see the link as a link unless they ask. Tools such as ls -l, stat, and find -type l can tell them apart. The option -L in many commands tells them to follow links.
Device files
A character device transfers data one byte at a time, such as a terminal or a random number generator, and a block device transfers fixed-size blocks, such as a disk. Each device file contains a major number, which selects the driver, and a minor number, which selects the specific device that the driver manages. You rarely create device files by hand on a modern system, because the kernel and the device manager create them when hardware appears. Creating one requires privileges. The unprivileged lab cannot make new device nodes, but it can read the existing ones.
The device /dev/null is the simplest example. It accepts writes and discards them, and reads return end of file. The device /dev/zero returns an endless stream of zero bytes, which is useful for making test files of a chosen size. Both are character devices.
Named pipes (FIFOs)
A named pipe is created with mkfifo. It appears in the directory tree with the type p, but it holds no data on disk. Opening it for reading blocks until a writer opens it, and the reverse. The kernel moves bytes from the writer to the reader through memory. A FIFO gives two unrelated programs a rendezvous point in the filesystem, which an anonymous pipe made by the shell cannot do. Anonymous pipes, the vertical bar between commands, are covered in the process module.
cd /workspace
mkfifo demo-pipe
ls -l demo-pipe
stat -c '%F' demo-pipeSockets
A Unix domain socket is an endpoint for communication between processes on the same machine. It appears as a file of type s, and programs connect to it by name. The Docker client and many system services use this mechanism, and you will often see sockets under /run. The socket file does not contain the messages. The kernel handles them in memory, and the file is only the rendezvous point. A socket exists only while a process has it open, so a stale socket file left behind by a crashed program can block a new one from starting.
When a command reports that it expected a regular file and got something else, run stat -c %F on the path first. It tells you immediately whether you have a device, a pipe, a socket, or a link.
Why the type matters to a program
Most programs check the type before they act, and many fail with a message that looks confusing until you know the type. A tool that copies a file reads from it until end of file, so pointing it at a FIFO blocks until a writer appears, and pointing it at a character device may read forever. A program that expects a directory and finds a symbolic link to a file must decide whether to follow the link. The type therefore decides whether a command is safe to run on a given path, and checking it first is a good habit in scripts.
The same reasoning applies to device files. Writing to the wrong device can destroy data, so a script that accepts a path from a user should check the type before it writes. The test -f accepts only regular files, which is a safer default than testing for existence alone.
Finding files by type
Later modules use find -type with the letters f, d, l, p, s, c, and b to select entries by type. The same letters appear in the long listing, so the vocabulary carries over. Use the type test whenever you want to act on files of one kind and leave the others alone.
Practice in the lab
In the lab you will create a named pipe, read the type of a device file, and then find a symbolic link by type. The checks look at the output that you save, so the evidence matches the type the kernel reports.