Linux file system is developed in such a way that simplifies storing, retrieving, and managing files. At some point in time, while using Linux, you would have read somewhere that Linux treats everything as a file. Linux has broadly categorized files into three parts: Regular files, Directory Files, and Special Files. This helps Linux manage the files and decide which applications can open or edit it.
In this guide, we will focus on different categories and subcategories of Linux files. We will also understand how to identify the file type. Lastly, we will summarize everything by tabulating whatever we grasped in this tutorial.
2. Regular files
Regular files are nothing but the everyday files used to store information such as text, or images. These files can be found in directories, which are yet another type of files. In Linux, regular files can exist with or without an extension.
ls -l /home/ubuntu/
We are listing all the files in the /home/ubuntu directory using the ls -l command. The results that begin with '-' will correspond to regular files.
3. Directory files
We know Linux follows a hierarchical structure to organize files. This is achieved using directories. Directories are also Linux files. But rather than storing data, they store the location of other files. To achieve this, 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 root (/) directory. All files and directory files are created under this directory. All Linux directories have a parent directory, except the root.
ls -l /home/ubuntu/ | grep ^d
We are listing all the files in the /home/ubuntu/ directory using the ls -l command. We are filtering the result to display the directory files by using the wildcard ^d. This will display those lines that begin with the letter 'd'.
4. Special files
The Linux special file can further be divided into five categories:
- Block file
- Character device file
- Named pipe file or just a pipe file
- Symbolic link file
- Socket file
4.1. Block file
These files are hardware files acting as a direct interface to block devices. A block device is any device that performs data input and output operations in units of blocks. A block special file represents a device that transfers data in blocks such as a hard drive. You can create these files by using the fdisk command or by partitioning. Linux places these files in the /dev directory.
ls -l /dev | grep ^b
We are listing block files in the dev directory using the ls -l command. We are filtering the result to display the block files by using the wildcard ^b. This will display those lines that begin with the letter 'b'.
4.2. Character device file
A character device file is a hardware file that reads or writes data one character at a time in a file. These files provide a serial stream of input or output and provide direct access to hardware devices. Terminal and serial ports come under this category.
ls -l /dev | grep ^c
We are listing all the files in the dev directory using the ls -l command. We are filtering the result to display the character files by using the wildcard ^c. This will display those lines that begin with the letter 'c'.
4.3. Named pipe file
Named pipe file also referred to as pipe file is sometimes called FIFO - First In, First Out. It works on the principle that the order of bytes going in is the same as coming out. The “name” of a named pipe is a file name within the file system. This file is responsible for sending data from one process to another so that the receiving process reads the data in FIFO fashion.
ls -l /dev | grep ^p
We are listing all the files in the dev directory using the ls -l command. We are filtering the result to display the pipe files by using the wildcard ^p. This will display those lines that begin with the letter 'p'.
4.4. Symbolic link file
A symbol link file is a special file in Linux that points to another file or a folder. Symbol link files are also called Symlink and are similar to shortcuts in Windows. Link files ensure that we have the flexibility to use a file with a different filename as well as from a different location.
A link file behaves like a pointer to another file. Hard link and soft link are the two types of links. A hard link develops a mirror copy of the original file. A hard link cannot be created for a directory or a file on another filesystem. But 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.
ls -l /dev | grep ^l
We are listing all the files in the dev directory using the ls -l command. We are filtering the result to display the link files by using the wildcard ^l. This will display those lines that begin with the letter 'l'.
4.5. Socket file
Applications use sockets to exchange data. A socket is just a communication endpoint that facilitates this exchange.
When an application wants to interact with another it has to connect with the socket of the other application. The application will use a socket to accept connections. Furthermore, every socket will have an IP address along with a port number associated with it. This allows it to accept connections.
Linux is on the constant lookout to smoothen the communication between local applications. For this it uses socket files. Socket files allow exchange of data without the complicated process of networks and sockets. These special files use a file name as their address instead of IP and port number.
ls -l /dev | grep ^s
We are finding all the files in the dev directory using the ls -l command and then filtering the result to display the socket files by using the wildcard ^s. This will display those lines that begin with the letter 's'.
5. Identification of file type
5.1. file command
The in-built tool named 'file' has the sole purpose of determining the file time in Linux.
This command only displays the type of the specified file but also shows the type of content stored in there.
5.2. ls -l command
The ls command is used to list the contents. Passed with the -l option, it gives the detailed list.
ls -l /dev
Using this command, we are listing the content of the current directory. The first character of each listing tells us about the type of file. The next section lists the symbol of different types of files along with all the other relevant information.
5.3. stat command
There is another useful tool, named stat, to figure out the information about the file and filesystem. It not only gives the file system information, but also file size, permissions, and user and group IDs.
In this example we are using the stat command on a regular file named records.csv, /etc/hosts (for this we have a symlink) and the /home/ubuntu directory. Stat command provides the filesystem information related to:
- Name of the provided file.
- Size of a given file in bytes.
- Total number of allocated blocks to the file to store on the disk.
- Size of every allocated block in bytes.
- File types - which could be regular files, special files, directories, or symbolic links.
- Device number in hexadecimal format.
- Inode number of the file.
- Number of hard links of the file.
- Access permissions in the numeric and symbolic methods.
- SELinux security context.
- Last time when the file was accessed.
- Last time when the file was modified.
- Last time when the file attribute or content was changed.
- File creation time.
6. Tabular summary
|Contains data of various content types such as text, script, image, videos
|ASCII, image, archive data etc
|Contains the name and address of other files
|Accesses block device I/O
|Associated with character or raw device access.
|Allows processes to send data to other processes or receive data from other processes.
|Points or mirrors other files
|Symbolic link to file
|Provides inter-process communication