How to Find Out File Types in Linux
Last Updated :
27 Sep, 2024
In Linux, everything is considered as a file. In UNIX, seven standard file types are regular, directory, symbolic link, FIFO special, block special, character special, and socket. In Linux/UNIX, we have to deal with different file types to manage them efficiently.
Categories of Files in Linux/UNIX
In Linux/UNIX, Files are mainly categorized into 3 parts:
- Regular Files: Standard files like text, executable, or binary files.
- Directory Files: Files that represent directories containing other files and folders.
- Special Files: This category includes block device files, character device files, symbolic links, pipes, and socket files.
The easiest way to find out file type in any operating system is by looking at its extension such as .txt, .sh, .py, etc. If the file doesn't have an extension then in Linux we can use file utility.
In this article, we will demonstrate file command examples to determine a file type in Linux.
file Command in Linux
The file command is used to determine the type of a file by analyzing its content, rather than relying solely on its extension.
To find out file types we can use the file command.
Syntax: file [OPTION...] [FILE NAME...]
Basic file Command Examples
You can run the following command to verify the version of the file utility:
file -v

We can test a file type by typing the following command:
file file.txt

We can pass a list of files in one file and we can specify using the -f option as shown below:
cat file.txt
file -f file.txt

Using the -s option we can read the block or character special file.
file -s /dev/sda

Using -b option will not prepend filenames to output lines
file -f GFG.txt

Using -F option will use string as separator instead of ":".
file -F '#' GFG.txt

Using -L option will follow symlinks (default if POSIXLY_CORRECT is set):
file -L stdin

We can use the --extension option to print a slash-separated list of valid extensions for the file type found.
file --extension GFG.rar

For more information and usage options, you can use the following command:
man file
We can also use ls command to determine a type of file.
Syntax:
ls [OPTION]... [FILE]...
The following table shows the types of files in Linux and what will be output using ls and file command
File Type | Command to create the File | Located in | The file type using "ls -l" is denoted using | FILE command output |
---|
Regular FIle | touch | Any directory/Folder | - | PNG Image data, ASCII Text, RAR archive data, etc |
---|
Directory File | mkdir | It is a directory | d | Directory |
---|
Block Files | fdisk | /dev | b | Block special |
---|
Character Files | mknod | /dev | c | Character special |
---|
Pipe Files | mkfifo | /dev | p | FIFO |
---|
Symbol Link Files | ln | /dev | l | Symbol link to <linkname> |
---|
Socket Files | socket() system call | /dev | s | Socket |
---|
Types of File and Explanation
1. Regular Files
Regular files are ordinary files on a system that contains programs, texts, or data. It is used to store information such as text, or images. These files are located in a directory/folder. Regular files contain all readable files such as text files, Docx files, programming files, etc, Binary files, image files such as JPG, PNG, SVG, etc, compressed files such as ZIP, RAR, etc.
Example:

Or we can use the "file *" command to find out the file type

2. Directory Files
The sole job of directory files is to store the other regular files, directory files, and special files and their related information. This type of file will be denoted in blue color with links greater than or equal to 2. A directory file contains an entry for every file and sub-directory that it houses. If we have 10 files in a directory, we will have 10 entries in the directory file. We can navigate between directories using the cd command
We can find out directory file by using the following command:
ls -l | grep ^d

We can also use the file * command

Special Files
1. Block Files:
Block files act as a direct interface to block devices hence they are also called block devices. A block device is any device that performs data Input and Output operations in units of blocks. These files are hardware files and most of them are present in '/dev'.
We can find out block file by using the following command:
ls -l | grep ^b

We can use the file command also:

2. Character device files:
A character file is a hardware file that reads/writes data in character by character in a file. These files provide a serial stream of input or output and provide direct access to hardware devices. The terminal, serial ports, etc are examples of this type of file.
We can find out character device files by:
ls -l | grep ^c

We can use the file command to find out the type of file:

3. Pipe Files:
The other name of pipe is a “named” pipe, which is sometimes called a FIFO. FIFO stands for “First In, First Out” and refers to the property that the order of bytes going in is the same coming out. The “name” of a named pipe is actually a file name within the file system. This file sends data from one process to another so that the receiving process reads the data first-in-first-out manner.
We can find out pipe file by using the following command:
ls -l | grep ^p

We can use the file command to find out file type:

4. Symbol link files:
A symbol link file is a type of file in Linux which points to another file or a folder on your device. Symbol link files are also called Symlink and are similar to shortcuts in Windows.
We can find out Symbol link file by using the following command:
ls -l | grep ^l

We can use the file command to find out file type:

5. Socket Files:
A socket is a special file that is used to pass information between applications and enables the communication between two processes. We can create a socket file using the socket() system call. A socket file is located in /dev of the root folder or you can use the find / -type s command to find socket files.
find / -type s
We can find out Symbol link file by using the following command:
ls -l | grep ^s

We can use the file command to find out file type:

Conclusion
In conclusion, understanding file types in Linux is essential for efficient system management and navigation within the operating system. The file and ls commands provide powerful tools for identifying file types, from regular files and directories to special files like block devices, pipes, and symbolic links.
Similar Reads
How to Run a File in Linux
The command line is one of the most powerful tools in Linux. It allows you to execute commands, manage files, and automate tasks all from a single terminal window. One common task you'll often need to do is run a file, whether itâs a script, a compiled program, or even a text file. In this article,
6 min read
How to Open a File in Linuxâ
In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everythingâfiles, directories, devices, and processesâas a file. Whether you're accessing a simple text docu
6 min read
How to Find Recently Modified Files in Linux?
Here we are going to see how to find recent or todayâs modified files in Linux. Locating files with a particular name is one of the problems Linux user's faces, it would be easier when you actually know the filename. let's assume that you have created a file in your home folder and you have forgotte
2 min read
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This articl
10 min read
How to Get the Full Path of a File in Linux
While dealing with files on Linux, especially shell scripts, one may require determining the full path of a file at times. Now, let's consider several methods of getting the full path of a file in Linux. In this article, we will be discussing several different solutions to a popular problem.Before w
3 min read
List One Filename Per Line in Linux
One of the most frequent tasks when using the Linux command line is listing the files in a directory. Sometimes we want the list of files to be in a specific format, such as one file per line. ls is a Linux shell command that lists directory contents of files and directories. It is one of the most o
3 min read
How to Create File in Linux
Today, we're going to learn about something really important â how to create files in Linux. It's like creating a fresh piece of digital paper to write or store things. We'll explore different ways to do this using simple commands. Whether you're just starting out or have been using Linux for a bit,
7 min read
How to Find Out the Virtualization Type in Linux System?
Determining the virtualization type in Linux is essential when you're working in a virtualized environment or managing multiple virtual machines. Whether you're using a hypervisor like KVM, Xen, or VMware, understanding how to find virtualization types in Linux can help you troubleshoot or optimize
4 min read
Display File System Type in Linux
Understanding the file system type on a Linux system is crucial for efficient management and troubleshooting. Different file systems serve various purposes, and identifying them provides insights into storage devices and their capabilities. This article explores various methods to display the file s
4 min read
How to Check Data Type in R?
R programming has data types that play a very significant role in determining how data is documented, modified, and analyzed. Knowing the data type of an object is an essential part of most data analysis and programming. The current article delivers a step-by-step guide that will familiarize you wit
4 min read