Linux File Command: What Does It Do and How to Use It

Linux File Command: What Does It Do and How to Use It

Download Complete Linux Cheat Sheet

Linux can be quite complicated as you need to use Linux commands to manage the operating system. The file command is one of them.

The file command has a wide range of uses. In essence, it checks the file type and reports back in a simple format. The command also states the reason why a file can’t be read and reveals the true file type in case it has been renamed.

In this tutorial, you’ll learn the basics of using the file command and how it can empower your VPS management and Linux operation.

What Exactly Does the Linux File Command Do?

File names in UNIX can be entirely independent of the file type. For example, a file named example.zip can actually be a text file instead of an archive as would be expected. In fact, files can have no extension at all.

This makes it tricky to determine the true file type. This is where the file command comes in handy. It reveals what type a file is – whether it’s an audio, open document spreadsheet, or text editor file.

The file command examines each argument and conducts three sets of tests to determine the file type. The first that succeeds will trigger an output with the file type.

  • Filesystem test – this test examines the return from the stat system call. The program reviews if the file is empty or if it’s a special file type. It also looks for any known file types relevant to the system you are working on if they’re specified in the header file.
  • Magic test – every file has magic numbers attached at the beginning. They’re special values in fixed arrangements that correspond to different file types. The file command has a database containing all magic numbers located in a data file /usr/share/misc/magic. When the file command is run, the system compares the magic numbers of the specified file with it.
  • Language test – this test examines the character sets the file is written in, such as ASCII or UTF-8. The test looks for special sequences that appear anywhere in the first few lines. This test is less accurate than the previous two tests, so it’s performed last.

Understanding Linux File Command Syntax

To use the file command on your VPS hosting, you’ll have to connect to it with an SSH client such as PuTTY.

Once you’re connected, you can use the file command on the terminal. Before you begin, however, you should understand the syntax of the command:

file [options] [file name]
  • file – instructs the terminal to execute the file command.
  • [options] – this is where you can add variables to the command.
  • [file name] – input the file you want to inspect.

The output of the file command displays the file type in a standard format. It may also provide other information, such as data stored in the compressed file, size, or the file version depending on the options you used.

To demonstrate the Linux file command, we’ll create a sample text file. The following command will work if you have the Nano text editor installed:

nano test.txt

The command line will open the new file in the Nano text editor. Write a few lines of text, then press CTRL+X and Y to exit and save the file.

Now let’s use the most basic form of the file command to check the text.txt file:

file test.txt

In the output, we’ll be able to see that it is a text file in the ASCII format.

Output showing that it is a text file in the ASCII format.

Ways to the Use the Linux File Command

The file command syntax doesn’t limit its use to just checking a file type. As you may have noticed, there’s the [options] section in the syntax where you can add extra variables. Including this option will enable you to perform different tasks with the file command

There are many options you can use with the Linux file command, but here is a quick overview of the most commonly used ones:

  • -b – fetches a short description of the file type.
  • file * – lists the types of all files in the directory.
  • -i – shows the MIME file type.
  • -s – used for special files.
  • -z – looks inside compressed files.
  • –help – opens the manual for the file command. You’ll see more options and their uses.

We’ll now take a detailed look at each option and how to use it.

Check the File Type

The file command in Linux is valuable as filenames in UNIX do not necessarily have a connection to their file type. For example, there may be a test.zip file which the user has renamed as test.csv. In this case, the following command can find out the true file type:

file test.csv

The output will define that the test.csv file is actually a .zip file:

Output showing that the test.csv file is actually a .zip file.

This command outputs the file name and its type. If you only want to view the file type, use the -b option in the terminal along with the file name:

file –b test.txt

The output only states ASCII text without the file name.

The output stating only ASCII text without the file name.

List the File Type of Multiple Files

The file command can also work with multiple files available on the system, with the output for each file in a separate line. To do this, simply replace a variable with the wildcard *:

file *.txt

The output will contain the information on all .txt files in the current directory.

Output showing information on all .txt files.

You can use this command to list all the files inside a directory by adding the directory name before the *. You don’t have to navigate to the directory in question.

For example, if you want to list all ASCII text files inside the Test directory, you can use the following command from root or any other directory:

file Test/*.txt

The output will look like this:

Output showing all .txt files inside the Test directory.

Determine the MIME File Type

The i option is used to view the MIME file type. MIME is a standard way of classifying file types on the internet and consists of two parts – types and subtypes.

The command will look like this:

file –i test.txt

Here’s the output for the above command:

The output for the MIME file command type.

Notice that instead of declaring the file format as ASCII text, it defines the file as text/plain and charset=us-ascii.

Read Special File Type

The file command is handy to analyze regular files. However, it can’t read block or character special files. If you use the file command on one of them, it will only tell you that it’s a block or character special file.

If we use the simple file command on /dev/ploop19269, a block special file, the output will look like this:

Output stating block special after using the simple file command on /dev/ploop19269.

To read these special files, use the -s option:

file –s /dev/ploop19269

As you can see, the output is much more detailed. It indicates that ploop19269 is a DOS/MBR boot sector.

A much more detailed output after using the file command with the -s option.

Read Inside a Compressed File

There are two ways to read inside compressed files like .zip or gzip archives. The -z option is used to find out information about the contents of compressed files along with the compression details. Using the option -Z will only show the contents.

Here’s the command for reading full compressed file details:

file -z test.gz

Below is the output. Notice that it specifies that test.gz is a .gzip compressed file that contains file.txt.

Output specifying that the test.gz file is a .gzip compressed file that contains file.txt.

Now let’s compare it to the -Z option:

file -Z test.gz

This command will only print out the type of the file inside test.gz – ASCII text.

Output only showing the file type inside the test.gz file.

Conclusion

The Linux file command helps users correctly identify files. This is especially useful as file names and extensions can be completely different from the actual file types on UNIX-like systems.

Be sure to use appropriate options and specify the correct file name when using the file command. The Linux terminal is case-sensitive, so be mindful of the uppercase and lowercase letters.

We hope this article has helped you learn more about different file types on your system and improve the development of your projects.

Was this tutorial helpful?

Author
The author

Edward S.

Edward is a Content Editor with years of experience in IT as a writer, marketer, and Linux enthusiast. Edward's goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Author
The Co-author

Leonardus N.

Leo is a Digital Content Writer at Hostinger. He loves to share his web hosting and WordPress knowledge to help people build a successful online presence. During his free time, he likes to play music and learn audio engineering.