This tutorial explains different types of files in Linux. Learn the types of files that Linux supports and how to identify a file type in Linux.
A file type helps us in identifying the type of content that is saved in the file. Linux supports seven different types of files. These file types are the Regular file, Directory file, Link file, Character special file, Block special file, Socket file, and Named pipe file.
The following table provides a brief description of these file types.
File type | Description |
Ordinary or regular files | Contain data of various content types such as text, script, image, videos, etc. |
Directory files | Contain the name and address of other files. |
Block or character special files | Represent device files such as hard drives, monitors, etc. |
Link files | Point or mirror other files |
Socket files | Provide inter-process communication |
Named pipe files | Allow processes to send data to other processes or receive data from other processes. |
Let's understand these file types in detail.
Regular or ordinary files
Regular or ordinary files store data of various content types such as text, audio, video, images, scripts, and programs. There are hundreds of content types. In Linux, regular files can be created with or without an extension.
An extension is a group of characters that is used with the file name to give it a special identity or to group it with files of the similar content type. For easy recognition and processing, files of different content types often use well-known file extensions.
Although the Linux file system does not need file extensions, still you should use them. They help us in identifying the types of content that are stored in files. For example, if a file has a .mp4 extension, you may know that it is a video file.
To view a complete list of content types and file extensions that your Linux system supports, you can see the /etc/mime.types file. The MIME (Multipurpose Internet Mail Extensions) provides a standard designation and classification for file content types.
You can use any standard file reading command to view the content of this file. For example, the following syntax uses the cat command to read this file.
$cat /etc/mime.types
The following image shows the output of this command.
Linux file system is mainly designed to store, retrieve, and manage regular files. Typically, all Linux distributions provide a dedicated directory to each user for storing regular files. This directory is known as the user's home directory. A user can store regular files in his home directory.
The following image an example of the home directory.
Directory files
To organize files in a hierarchy, file systems use directories. Directories are also files, but instead of storing data, they store the location of other files. To store the location of files placed in the directory, the directory uses directory entries. Each directory entry stores the name and location of a single file.
Linux file system starts with a directory called / or root directory. All files and directory files are created under this directory. Except the root directory, each directory has a parent directory.
Special files
Linux treats all hardware devices (such as hard drives, printers, monitors, terminal emulators, and CD/DVD drives) as special files. This means that an application program can access and use files and devices in the same way. This feature makes developing programs in Linux easier and flexible.
Linux places all special files or device files under the /dev directory. There are two types of special files: a character special file and a block special file. A character special file represents a device that transfers data in bytes such as a monitor or a printer. A block special file represents a device that transfers data in blocks such as a hard drive.
Link files
Link files allow us to use a file with a different filename and from a different location. For this, we use link files. A link file is a pointer to another file. There are two types of links: a hard link and a symbolic or soft link.
A hard link creates a mirror copy of the original file. A hard link cannot be created to a directory or a file on another filesystem. A soft or symbolic link creates a pointer to the original file. A soft link can be created to a directory or a file on another filesystem.
Socket files
A socket is a communication endpoint that applications use to exchange data. For example, if an application wants to communicate with another application, it connects with the socket of that application.
Each application that provides services to other applications or remote clients uses a socket to accept connections. Each socket has an associated IP address and port number that allow it to accept connections from clients.
For example, if an application of the local system wants to communicate with another application of a remote system, it connects to the socket of that application by using the associated IP address and port number of that socket.
Sockets are very complicated. To make the communication process easier between local applications, Linux uses socket files. Socket files allow applications of the local system to exchange data without going through the complex process of networking and sockets.
Socket files are the special files that use a file name as their address instead of an IP address and port number. Socket files use the sendmsg() and recvmsg() system calls to enable inter-process communication between local applications.
Named pipe files
Linux allows us to send the output of any process or command to another process or command as the input. This feature is known as the pipe. Pipes work only when both processes are started by the same user and exist in the same parent process space.
If processes are executed under different user names and permissions, then standard pipes do not work. In such circumstances, named pipes are used. Named pipes are similar to the standard pipes except that they can be accessed as part of the filesystem.
Named pipe files are the empty pipe files. The kernel processes named pipe files without writing them to the file system. Named pipe files can exist anywhere in the file system. Named pipe files are also known as the FIFO (First In First Out) files.
How to identify the type of a file?
There are many ways to identify the type of a file in Linux. The easiest way is to use the file command. To find the type of a file, specify the name of that file as an argument. For example, to know the file type of the file abc, use the following command.
$file abc
The output of this command not only displays the type of the specified file but also shows the type of content stored in the specified file.
Let's take a simple example. Login from a regular user account and copy some different types of files in the home directory. Now access a shell prompt and use the file command to identify the type of each file.
The following image shows this exercise with the output.
You can also identify the type of file by looking at the output of the ls -l command. The ls -l command lists the contents of the specified file. For example, the following command lists the content of the current directory.
$ls -l
The following image shows the sample output of the above command.
In the output listing, the first character of each listing tells the type of the file. The following table lists the symbol of different types of files.
Character | Meaning |
- | Regular or ordinary file |
d | Directory file |
l | Link file |
b | Block special file |
p | Named pipe file |
c | Character special file |
s | Socket file |
That's all for this tutorial. If you like this tutorial, you may consider to share it with friends through your favorite social network.