Kahibaro
Discord Login Register

1.5.3 File types

Understanding File Types in Linux

In Linux, everything is treated as a file. This simple idea hides an important detail: not all files are the same type. The system uses different file types to represent regular data, directories, devices, communication channels, and more. Knowing these types helps you understand command outputs, troubleshoot permissions, and safely manipulate the filesystem.

This chapter focuses on how Linux represents file types and how you can recognize them as a user.

How Linux Represents File Types

Internally, Linux tracks each file with metadata that includes its type and permissions. When you run commands like ls -l, you see a human readable summary that begins with a single character indicating the file type, followed by the permission bits.

The general pattern looks like this:

-rw-r--r--  1 user user  1234 Jan  1 12:00 example.txt
drwxr-xr-x  2 user user  4096 Jan  1 12:00 mydir

The very first character is the file type indicator, and the next nine characters are the permission bits. The type indicator is crucial.

Important rule:
The first character in ls -l output shows the file type:
- regular file, d directory, l symbolic link, c character device, b block device, p named pipe, s socket.

In the following sections, you will see what each of these types means in practice.

Regular Files

A regular file is the most common file type. It usually contains user data, such as text, images, compiled programs, or any binary data. In ls -l, a regular file is indicated by - as the first character.

For example:

-rw-r--r-- 1 user user  532 Jan  1 12:00 notes.txt
-rwxr-xr-x 1 user user 8192 Jan  1 12:00 myprogram

Both notes.txt and myprogram are regular files. The difference between a "normal" data file and an executable program is not in the type character but in the executable permission bits and the file contents. Linux does not use file extensions to define file types at the system level. Instead, it relies on permissions and file metadata.

From a user perspective, regular files are the files you edit, copy, and open most often.

Directories

A directory is a special file that contains a list of entries, which map names to other files or directories. In ls -l, directories start with d.

Example:

drwxr-xr-x 2 user user 4096 Jan  1 12:00 Documents

The directory is itself a file that stores information about which names exist inside it and to which inodes they point, but you interact with it through commands that list, create, or remove its contents.

Unlike regular files, directories are containers. You cannot "edit" a directory as text because its structure is managed by the filesystem. You interact with it by creating and removing entries within it.

Symbolic Links

A symbolic link, often called a symlink, is a special file that contains a path to another file or directory. It acts as a shortcut or reference. In ls -l, symbolic links start with l.

Example:

lrwxrwxrwx 1 root root   7 Jan  1 12:00 lib -> lib64

The -> in the listing shows the target of the link. The symbolic link itself has its own permissions and path, but when you access it, the system follows the stored path to the target.

Symlinks are particularly useful when you want multiple paths to refer to the same file or directory without copying data. If the target is removed, the link becomes "broken" and points to a non existent path.

Device Files: Character and Block

Linux represents hardware devices as special files, mainly located in /dev. There are two main device file types that you will see: character devices and block devices. This is part of how Linux unifies access to hardware using the "everything is a file" idea.

Character Device Files

A character device file provides unbuffered, character by character access to hardware or a virtual device. In ls -l, these files start with c.

Example:

crw-rw-rw- 1 root root 1, 3 Jan  1 12:00 /dev/null

The numbers like 1, 3 are the major and minor device numbers, which identify the specific driver and device instance. You usually do not modify these numbers manually as a normal user.

Character devices are used for things like serial ports or pseudo terminals where data is processed as a stream of characters.

Block Device Files

A block device file provides buffered, block oriented access, typically for storage devices like disks and partitions. In ls -l, these start with b.

Example:

brw-rw---- 1 root disk 8, 0 Jan  1 12:00 /dev/sda

Through these files, the system reads and writes data in blocks rather than characters. While you will not usually interact with device files directly as a beginner, you will see them when working with disks or during installation and troubleshooting.

Important distinction:
c at the start of ls -l output marks a character device file.
b at the start marks a block device file.

Named Pipes (FIFOs)

A named pipe or FIFO (First In, First Out) is a file type used for inter process communication. In ls -l, named pipes start with p.

Example:

prw-r--r-- 1 user user 0 Jan  1 12:00 mypipe

Processes can open this file and send data through it. One process writes to the pipe and another reads from it. The data flows in the order it was written, which is where the term FIFO comes from.

Unlike anonymous pipes that you create temporarily with shell operators, a named pipe has an entry in the filesystem, so unrelated processes can find and use it by name.

Sockets

A socket file is another inter process communication mechanism. Sockets often use network style communication, but the socket files you see in the filesystem provide local communication endpoints on the same machine. In ls -l, socket files start with s.

Example:

srw-rw-rw- 1 root root 0 Jan  1 12:00 /run/docker.sock

Processes can connect to this socket file to exchange data, usually in a structured protocol. Many system services expose local control interfaces as Unix domain sockets instead of network ports for security and performance reasons.

How to Identify File Types from the Command Line

Besides ls -l, there are simple commands and options that help you identify file types more directly.

The ls command with -F adds markers to filenames that hint at their type. For example:

ls -F

This may show directories with a trailing /, executables with a *, symbolic links with @, and so on, depending on your system configuration.

You can also use the file command:

file example.txt

This does not show the filesystem type character. Instead, it inspects the file contents and metadata to guess what the file actually contains, such as "ASCII text", "ELF 64-bit LSB executable", or "JPEG image data". This is different from the basic filesystem type, but both perspectives are useful.

Key idea:
ls -l shows the filesystem type (regular, directory, link, etc.).
file tries to describe what kind of data the file holds (text, image, executable, and so on).

Special Attributes vs File Types

Sometimes you will see extra symbols in long listings, such as a + at the end of the permission bits. For example:

-rw-r-----+ 1 user user 123 Jan  1 12:00 report.txt

The + does not mean a different file type. It indicates additional attributes, such as Access Control Lists. These are separate from the basic file type indicator. The actual type is still determined only by that very first character.

Similarly, file extensions like .txt, .sh, or .jpg are not used by Linux to decide the filesystem file type. Extensions are a human convention and are sometimes used by desktop environments or applications, but the kernel itself cares about the metadata and permissions, not the extension.

Why File Types Matter to You

Understanding file types helps you avoid mistakes and interpret what you see in the system.

If you know that d means a directory, you will not try to open it in a text editor. If you see l at the beginning of a listing, you know that modifying the symlink target may affect multiple paths at once. When working with system directories like /dev and /run, recognizing c, b, p, and s helps you understand that these entries are not normal files to edit, but interfaces to devices or services.

As you progress, you will use file types to select files in scripts, to understand error messages, and to diagnose configuration problems. For a beginner, it is enough to recognize each type in ls -l output and to know that not all "files" are equal inside Linux.

Views: 68

Comments

Please login to add a comment.

Don't have an account? Register now!