How to Find Out File Types in Linux

LinuxOpen SourceOperating System

When it comes to Linux operating system, everything in general can be considered as a file. In UNIX, in total, there are seven standard file types −

  • FIFO special

  • Block special

  • Character special

  • Socket

  • Regular

  • Directory

  • Symbolic link

When it comes to Files, these can be mainly categorized into the following categories −

  • Regular Files

  • Directory Files

  • Special Files

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 −

file -v

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.

file mybashfile.sh

On execution, it will produce the following output on the terminal −

mybashfile.sh: ASCII text

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 −

file -f sample.tar.gz

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 −

file -s /dev/sda1

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 −

file -Z sample

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 −

file -i -s mybashfile.sh

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 −

sample.tar.gz: ??

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.

raja
Updated on 19-Jul-2022 15:03:50

Advertisements