Linux file Command | Explained

In Linux, the file command is used to retrieve the file type and the data associated with the file. The operations performed by the file command are irrespective of the file extension. This post will demonstrate the brief working and usage of the file command in Linux. The content that supports this is as follows:

Let’s get started!

What is the file Command in Linux?

Whenever you are trying to learn about a new command, you need to understand the syntax and the relevant options of the command. The syntax of the file command is provided below:

Syntax

How to install Kali Linux in Window...

The syntax of the file command is mentioned below, which will help you understand.

file [Options…] [file…]

For details on options and their application, you can type the following command, whereas the file could be any file on which you are applying this command:

$ file --help

Doing this, you will be able to understand its basic syntax and the flags information associated with it.

How to Use the file Command in Linux?

This section provides a list of examples representing the possible usages of the file command in Linux. Let’s dig into them one by one:

Check the Status of an Empty File

We have an empty file; let’s check how the file command behaves in the case of an empty file:

$ file testfile.txt

As you can see, it shows that there is nothing inside the file, and the text file is empty.

Check the Status of a Non-Empty File

Now, the same file is equipped with some content. Let’s execute the file command on the file (a nonempty file):

$ file testfile.txt

It is observed that the file format is “ASCII.

Check the Status By Providing the Path of the File

The above command will execute only if you are in the same directory, so if you want to check the status of any file in a different directory, then you can do that by typing:

$ file /etc/adduser.conf

Check the Status of a Directory Using file Command

As you have checked the status of the files above, in the same way, you can check the status of any directory as well by typing:

$ file /home/itslinux/Downloads

In the above command, you need to provide the path of any directory for which you want to check the status.

Check the Status Only Without the Path/Name

If you are only interested in finding the status of a file or a directory, then you need to use the ‘-b’ flag as we did in the following command:

$ file -b testfile.txt

Similarly, the same you can do this with the directory as well by typing:

$ file -b /home/itslinux/Downloads

Check the Status of All files and Folders in the Directory

Checking the status of files and folders individually is time-consuming. Let’s suppose you want to check the status of the home directory then you can do that by typing:

$ file *

You can see in the above image that most of them are the directories, whereas there is one empty file and one file with the ASCII text on it.

Check the Status of the files/Directories in Sub-Directories

Similarly, you can check the status of the files/sub-directories of any directory using the file command. For instance, in the case of the “Documents” directory, the command would be:

$ file Documents/*

That’s all from this post!

Conclusion

As a Linux user, you might come across different types of files in your daily life, which can confuse you. If you want to figure out the type of file and its operation, then you can do that using a file command that can either be an ASCII file or a MIME. So the details about the file command and how you can utilize it in a better way have been discussed in this article.