- How to Find Out File Types in Linux
- Types of File and Explanation
- Regular Files
- Directory Files
- Special Files
- 1. Block Files:
- 2. Character device files:
- 3. Pipe Files:
- 4. Symbol link files:
- 5. Socket Files:
- How to Find Out File Types in Linux
- Example 1
- Example 2
- Example 3
- Example 4
- Example 5
- Example 6
- Example 7
- Conclusion
- How to Find Out File Types in Linux
- Linux file Command Examples
How to Find Out File Types in Linux
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.
In Linux/UNIX, Files are mainly categorized into 3 parts:
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.
To find out file types we can use the file command.
Syntax: file [OPTION…] [FILE…]
You can run the following command to verify the version of the file utility:
We can test a file type by typing the following command:
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.
Using -b option will not prepend filenames to output lines
Using -F option will use string as separator instead of “:”.
Using -L option will follow symlinks (default if POSIXLY_CORRECT is set):
We can use the –extension option to print a slash-separated list of valid extensions for the file type found.
For more information and usage options, you can use the following command:
We can also use ls command to determine a type of 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 |
Socket Files | socket() system call | /dev | s | Socket |
Types of File and Explanation
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.
Or we can use the “file *” command to find out the file type
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:
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:
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:
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:
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:
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.
We can find out Symbol link file by using the following command:
We can use the file command to find out file type:
How to Find Out File Types in Linux
The simplest way to find out what a particular file is in an operating system is by looking at what kind of extension that particular file has. The extensions can be ‘.txt’, ‘.py’, ‘.go’, etc. If a particular file does not have any extension in Linux, then we basically call that file as ‘file utility’.
In this tutorial, we will show how you can find out the type of different files that are present in Linux by using different Linux commands.
If a particular file does not have any type in Linux, then we can use a Linux file utility called ‘file‘ which is used to carry out some basic test on a specific file and then it basically prints the file type.
Now that we know how we can basically print the type of a file, let’s take a few examples and see how it works.
Example 1
In the first example, we will use the ‘file’ command with a default option ‘-v’ which will verify what kind of version this file is using of the file utility.
Consider the command shown below −
Run the above command on the terminal and it will produce the following output −
file-6.11 magic file from /etc/magic:/usr/sample/misc
In the above output, you can see that the first line basically prints the version of the file utility that the file is using and in the next line, we get the detailed information of the file.
Now that we know a little about the ‘file’ command, let’s use it in different examples.
Example 2
In this example, we will use the ‘file’ command and pass a file as an argument and then we will run the command.
On execution, it will produce the following output on the terminal −
Note − Note that the above output is particular for my machine and it may vary on your machine.
Example 3
In case you want to examine a file per line, then you can use the ‘-f‘ option that is available to us in the ‘file’ command. Consider the command shown below −
Run the above command and you will get to see the following output −
sample.tar.gz: bzip2 compressed data, block size = 900k
Example 4
As we know, by default, the ‘file’ command will run some tests to determine the type of extension of a particular file, and if you want to skip these steps, then you can use some default options. Then the command will run even faster, as it will skip all the unnecessary tests and we can provide some sample tests to skip as well.
Consider the command shown below.
file -e ascii -e reduce-e elf sample
In the above command, we are using the ‘-e’ option and then we pass the name of different tests that we want to skip in order to make the command run even faster.
By default, there is also a different option that we can use when we want to read block or character special files.
Consider the command shown below −
The above file is a partition file which contains different blocks of memory and different partitions that we can use and we can print as well with the help of the ‘-s’ command option present inside the ‘file’ command.
Example 5
Let’s consider an example where we know that we have a compressed file and we want to know what kind of data is present inside that file. We can check the same with the help of the ‘file’ command and providing a default option ‘-z’ with it.
The ‘-z’ option is used to instruct the file that we want to look inside a particular compressed file.
Consider the command shown below −
file -z samplefile -z samplefile -z sample
In the above command, the filename ‘sample’ is a compressed file and if you run the above command on the terminal, you will get a detailed information about what kind of file it is and what kind of data it contains.
Suppose you want to print the information about the contents of a particular file (not the compression of the file), then you can use the ‘file’ command with the ‘-Z’ option −
If you run the above command on the terminal, you will only get the information about the data that is present in the ‘sample’ file.
Example 6
If necessary, you can instruct the ‘file’ command to only output the MIME type strings instead of the more traditional or the basic human-readable outputs that we usually get when we run the ‘file’ command.
In order to get the mime type strings as the output, you need to run the following command −
Once we run the above command in the terminal we will get the following output −
mybashfile.sh: text/plain; charset=us-ascii
Example 7
In case you want to print a slash-separated list of valid extensions for a particular file type, then use the following command −
file --extension sample.tar.gz
Once we run the above command in the terminal we will get the following output −
Conclusion
In this tutorial, we explained how you can use the ‘file’ command to know the type of a file with the help of different options.
How to Find Out File Types in Linux
The easiest way to determine the type of a file on any operating system is usually to look at its extension (for instance .xml, .sh, .c, .tar etc..). What if a file doesn’t have an extension, how can you determine its type?
Linux has a useful utility called file which carry out some tests on a specified file and prints the file type once a test is successful. In this short article, we will explain useful file command examples to determine a file type in Linux.
Note: To have all the options described in this article, you should be running file version 5.25 (available in Ubuntu repositories) or newer. CentOS repositories have an older version of file command (file-5.11) which lacks some options.
You can run following command to verify the version of file utility as shown.
$ file -v file-5.33 magic file from /etc/magic:/usr/share/misc/magic
Linux file Command Examples
1. The simplest file command is as follows where you just provide a file whose type you want to find out.
2. You can also pass the names of the files to be examined from a file (one per line), which you can specify using the -f flag as shown.
3. To make file work faster you can exclude a test (valid tests include apptype, ascii, encoding, tokens, cdf, compress, elf, soft and tar) from the list of tests made to determine the file type, use the -e flag as shown.
$ file -e ascii -e compress -e elf etc
4. The -s option causes file to also read block or character special files, for example.
$ file -s /dev/sda /dev/sda: DOS/MBR boot sector, extended partition table (last)
5. Adding the -z options instructs file to look inside compressed files.
6. If you want to report information about the contents only not the compression, of a compressed file, use the -Z flag.
7. You can tell file command to output mime type strings instead of the more traditional human readable ones, using the -i option.
$ file -i -s /dev/sda /dev/sda: application/octet-stream; charset=binary
8. In addition, you can get a slash-separated list of valid extensions for the file type found by adding the –extension switch.
For more information and usage options, consult the file command man page.
That’s all! file command is a useful Linux utility to determine the type of a file without an extension. In this article, we shared some useful file command examples. If you have any questions or thoughts to share, use the feedback form below to reach us.