CentOS Debian Manjaro Mint openSUSE Red Hat Ubuntu

How to Use a File Command in Linux


In Linux, file names can be completely independent of the file type. For example, instead of being an archive, a file named test.zip could be a text file. Also, any file may have no extension at all. This makes determining the true file type difficult. The file command comes particularly handy in this situation. It tells you whether a file is an audio file, an open document, a spreadsheet file, or a text editor file.

The file command evaluates each parameter and performs three sets of checks to identify the file type. The first one to succeed will produce a file type output. In the filesystem test, the application examines the file to see if it’s empty or if it’s a unique file type. It also checks the header file for any known file types that are relevant to the system you’re working on.

In the magic test, every file has a magic number tied to it at the start. They’re specific fixed-arrangement values that correspond to certain file types. The file command maintains a database of all magic numbers in the /usr/share/misc/magic data file. When you perform the file command, the system compares the provided file’s magic numbers to it.

Language test looks at the file’s character sets, such as ASCII or UTF-8. The test looks for peculiar sequences in the first few lines that exist anywhere. This test comes last since it is less accurate than the other two.

Different ways to use File Command

With the Linux file command, you have a lot of choices, but here’s a short rundown of the most popular ones:

-b: It retrieves a brief description of the file type.

file *: It returns a list of all the sorts of files in the directory

-i : It displays the MIME file type.

-z : It inspects compressed files.

–help : It displays the file command’s manual.

We’ll now go through each option in detail and show you how to use it.

Method 1: Check File Type

To create a new text file named “test”, run the following command:

$ touch test.txt

Text Description automatically generated

This command will create an empty text file, open the file and write some text in the file and then click on the “Save” option. Then, run the following mentioned command to check the file type of the “Test” file:

$ file test.txt

Text Description automatically generated

If you only want to see the file type, type -b in the terminal with the file name:

$ file –b test.txt

Text Description automatically generated

Method 2: Check the file type of multiple files

The file command can also function with many files on the system, with each file’s output appearing on its own line. To achieve this, simply run the following command in the terminal:

$ file *.txt

Text Description automatically generated

Method 3: Check MIME File Type

MIME is a two-part system for identifying file types on the internet. Types and subtypes are the two parts of this file. To check the MIME file type, simply run the below-mentioned command in the terminal:

$ file –i test.txt

Text Description automatically generated

It is worth noting that its output is quite different from the previous one .i.e. the file format was ASCII text for the previous cases but in this case, it’s text/plain; charset=us-ascii.

Method 4: Read inside compressed files

To accomplish the purpose of reading inside compressed files use the –z option. The -z option is used to retrieve information about compressed files’ contents and compression details. To do so, you can type the following mentioned command in the terminal:

$ file –z test.zip

Text Description automatically generated


The file command in Linux assists users incorrectly identifying files. This is especially useful on UNIX-like systems because file names and extensions might range dramatically from file types. When using the file command, make sure you use the necessary arguments and supply the correct file name. Be aware that the uppercase and lowercase letters in the Linux terminal are case-sensitive.

Similar Posts