How to Use the Linux Command file and Essential 4 Options

The Linux command file is incredibly useful when you need to identify the type of a file. For instance, if you have a file without an extension, it’s crucial to determine whether it’s a text file, an executable, or some other type of file. In this post, we’ll explore the basic usage of the file command, its various options, and how it can be practically applied in real-world scenarios.

What is the file Command?

The file command in Linux is used to identify the type of a file. It analyzes the content of the file to determine whether it’s a text file, a binary file, a script file, or any other type of file. Unlike methods that rely on file extensions or names, the file command examines the actual data within the file, providing more accurate information.

Basic Usage

Using the file command is straightforward. Simply enter the file command followed by the filename in the terminal.

file filename
ShellScript

For example, to check the type of a file named example.txt, you would use:

file example.txt
ShellScript

The file command will then tell you that the file is an ASCII text file. If the file is an executable, it will inform you of that as well. As shown below, you can identify various types of files, including audio files, video files, text files, compressed files, PDFs, PPTs, JSONs, and more.

Figure 1. Linux Command file: Execution result
Figure 1. Linux Command file: Execution result

Key Options for the Linux Command file

The file command has several options that allow you to retrieve more detailed information about a file. Below are some of the most commonly used options.

-i Option: Checking the MIME Type

The -i option displays the MIME type of a file. A MIME type is a standard format used on the internet to describe the type of a file. Using this option allows you to check whether the file is a text file, an image file, or some other format.

file -i example.txt
ShellScript

When you run this command, the MIME type of example.txt will be displayed as text/plain; charset=us-ascii. Here, text/plain indicates that the file is a plain text file, and charset=us-ascii shows that it uses ASCII encoding.

Figure 2. Linux Command file: Checking the MIME type with the -i option
Figure 2. Linux Command file: Checking the MIME type with the -i option

-b Option: Omitting the Filename

By default, the file command includes the filename in its output. However, using the -b option, you can omit the filename and display only the file type.

file -b example.txt
ShellScript

As shown below, this command outputs only the file type, which can be useful for keeping results clean when checking the types of multiple files at once.

Figure 3. Linux Command file: Omitting the filename with the -b option
Figure 3. Linux Command file: Omitting the filename with the -b option

-f Option: Checking File Types from a List

The -f option is handy when you need to check the types of multiple files at once. By specifying a file that contains a list of filenames, this option allows you to check the type of each file in the list.

file -f filelist.txt
ShellScript

If the filelist.txt file contains multiple filenames, the file command will output the type of each file sequentially. As shown below, you can see how files listed in list.txt are identified using the -f option.

Figure 4. Linux Command file: Checking types of files listed with the -f option
Figure 4. Linux Command file: Checking types of files listed with the -f option

A symbolic link is a special type of file that points to another file or directory. When using the file command to check the type of a symbolic link, you can use the -L option if you want to check the type of the file it points to, rather than the link itself.

file -L linkname
ShellScript

As shown below, this option will tell you the type of the actual file that the symbolic link points to.

Figure 5. Linux Command file: Identifying the original file type of a symbolic link with the -L option
Figure 5. Linux Command file: Identifying the original file type of a symbolic link with the -L option

Practical Uses of the file Command

Automating File Checks

When managing systems or writing scripts, you can use the file command to automate the processing of specific file types. For instance, if you need to create a script that only processes text files, you can use the file command to identify file types and then perform certain actions based on the results.

#!/bin/bash

for file in *; do
  if file "$file" | grep -q "text"; then
    echo "$file is a text file."
  fi
done
ShellScript

This script runs the file command on all files in the current directory and prints a message if a file is identified as a text file.

Figure 6. Linux Command file: Using a script to process files based on type
Figure 6. Linux Command file: Using a script to process files based on type

Detecting Malicious Code

If you suspect that a file might be malicious, you can use the file command to detect unexpected file types. For example, a file with a .jpg extension might actually be an executable file. This method can help you preemptively scan files to enhance security.

Caution When Using the file Command

  • File Extensions vs. Actual File Type: The file command ignores file extensions and analyzes the actual file data, making it more reliable than judging file types based solely on filenames. For instance, a file with a .txt extension might actually be a binary file.
  • Be Careful with Large Files: Running the file command on large files can take a long time. This is especially true if the files are on a network drive, as network conditions can further delay the process.
  • Accuracy May Vary: In some cases, the file command might incorrectly identify the type of a file. For example, complex formats or compressed files might yield unexpected results, so it’s a good idea to perform additional verification when necessary.

Summary

Identifying the type of files in Linux is a crucial task in system management and security. The Linux command file is a simple yet powerful tool that analyzes file content to accurately determine its type. By leveraging various options, you can obtain more detailed information, automate scripts, or enhance security.

Whenever you need to check file types, make sure to use the Linux command file. A small effort in checking file types can prevent bigger issues down the road.

References

Leave a Comment