Knowledge Base
MilesWeb / VPS FAQ

Steps to Use the Linux File Command

Approx. read time : 4 min

There are several Linux commands that are useful for beginners and experienced too. One of the commands is the Linux file command. This typical application in Linux decides what kind of data is saved in a specific system file. It describes the file type in a format that is understandable, otherwise known as a MIME type. In this tutorial, you will learn the basics of using the Linux file command, and the way it can authorize your VPS management.

Filenames in UNIX can be completely free of the file type as the Linux file command will easily disclose what the type is. May it be an audio, open document spreadsheet or text editor file, the Linux file command will displays its type in a readable form.

How to Use the Linux File Command?

This command can be used for various things. It helps to understand the reasons behind the users’ inability to read a certain file on the system. Additionally, it also helps to determine the type of folder, socket, FIFO (pipes) and helps block special files. Only zero-length files in the Linux system are viewable by File command.

Linux File Command Syntax

Make sure that you access your VPS with SSH, before using the command. The basic syntax for the file command is as below:

  • file – It informs the terminal to run the Linux file command.
  • [options] –Here you can add variables to the command.
  • [filename] – It is the file you want to check by using the command.

After you run the command, the system will auto-reply you, mentioning the type of file in a standard output format.

You will also be provided with other beneficial information, like data stored in the file, the file size or the file version that you have entered.

Here, we created a simple text.txt file. Below is the most basic form of the Linux file command:

The output displays that the file is an ASCII text file.

What to use the Linux File Command for?

With the File command each argument is analyzed for classifying it. Three sets of tests are conducted in this sequence: file system tests, magic tests, and language tests. If the initial analysis succeeds, it provides the file type.

The file system tests are conducted by the users to study the return from a stat system call. The program checks if the file is clear, or if it’s some kind of special type of file. It checks for file types that are known, relevant to the system you are working on, if those are specified by the system in the system header file.

Files with data fixed arrangements are controlled with the application of magic tests. Usually a database is used to manage the probing of lead bytes by the file command. A file termed as magic is where that database is implemented and it is located in devisers/file/magic or a similar one. These files comprise of a magic number saved in a particular place near the start of the file that displays Linux and other operating systems that the file is a binary executable.

After determining the character set, it will then try to specify the language in which the file is written. The language tests find for special sequences that can display from anywhere in the first few segments of a file. These tests are not completely positive as compared to the previous two groups, so these are performed later.

Using the Linux File Command with Options

Since the filenames in UNIX don’t have any connection with their file type, the file command seems to be valuable.

Therefore, a file termed as devisers.csv could be a zip file. You can test this with the below command:

In case you just want to view the file type then use the –b option in the terminal along with the filename:

The file command has the ability to work on multiple files available in the system and return the output in separate lines for each file. For this, you only need to replace a variable with a wildcard – *:

The information will be displayed on all .txt files in the current directory.

You can view the mime type of the file which is not in a readable format with the –i option. The command would appear as below:

To see the special files, use –s with which the user can read the argument files which are not working or contain special characters. The information about the size of the special files is also specified. You can view the special file with the below command:

Make sure to add the name of the actual file you want to inspect!

Summary

From the article, it’s clear that the Linux file command is a useful utility for generating long algorithms in various ways according to your needs. You simply need to specify the file and use the command in the correct syntax.

You can understand the things you have on your machine in a precise manner with this command.

After you get used to, to this command, you will learn more about different file types present in your system. This will successfully improve your project. Happy developing!

Need help? We’re always here for you.