1. Introduction

The file command in Linux examines the content of a file to determine its type. It helps users understand the nature of files, facilitating tasks such as file management security analysis and troubleshooting. Additionally, it recognizes a wide range of file types, including text files, executables, and multimedia files, thus, providing valuable insights independent of file extensions or metadata.

In this tutorial, we’ll explore the usage and common options of the command. Finally, we’ll discuss some common practical examples.

2. Common file Command Option

Let’s look at the basic syntax of the file command:

$ file [option] [filename]
  • [option] –  the various command-line options of the file command
  • [filename]  – the name(s) of the file(s) to be processed

Additionally, the file command utilizes a database of patterns known as the magic file to analyze the content of a file and determine its type. This database contains a collection of signatures or magic numbers that help identify various file formats based on their unique characteristics.

Now, let’s explore the various options:

Options

Description

–b, –brief

Display only the file type in brief mode

-c

Display a checking printout of the parsed form of the magic file

-f

Read the names of the files to be examined from a specified file before the argument list

-F

Separate file and file type with a specified separator (default is ‘:’)

-i

View the MIME type of the file

-N

Do not pad filenames to align in the output

-s

Display information for special files

-z

Attempt to look inside compressed files

3. Common file Command Examples

Let’s delve into practical examples of using file commands.

3.1. Basic Usage

To start with, we run the command without an option. Let’s check the file type of a file called wget-log by using the command with just the filename:

$ file wget-log
wget-log: ASCII text

The output shows that the file type of wget-log is ASCII text.

3.2. Displaying Types of Multiple Files

We can also use the command to check the file type of multiple files by specifying the files one after the other:

$ file docker-compose.yml final_work.drawio cred.py test copy_multiple.sh 
docker-compose.yml: ASCII text
final_work.drawio:  Unicode text, UTF-8 text, with very long lines (424)
cred.py:            ASCII text
test:               directory
copy_multiple.sh:   Bourne-Again shell script, ASCII text executable

This reveals that docker-compose.yml is an ASCII text file and cred.py is an ASCII text file. Additionally, it identifies final_work.drawio as a Unicode text file with very long lines, test as a directory, and copy_multiple.sh as a Bourne-Again shell script, ASCII text executable.

3.3. Using the -N Option

By including the -N option, we instruct the file command not to pad filenames for alignment in the output:

$ file -N docker-compose.yml final_work.drawio cred.py test copy_multiple.sh
docker-compose.yml: ASCII text
final_work.drawio: Unicode text, UTF-8 text, with very long lines (424)
cred.py: ASCII text
test: directory
copy_multiple.sh: Bourne-Again shell script, ASCII text executable

The output remains the same, with the file types identified, but without any padding applied to align filenames. This option offers a more concise display of file information, particularly when alignment is unnecessary.

3.4. Using the -b Option

Let’s now explore the -b option, which allows us to display only the file type in brief mode:

$ file -b wget-log
ASCII text

With this option, we obtained brief information about the file type without additional details. In particular, it displays only the file type of the wget-log file.

3.5. Using the -c Option

Next, let’s explore the -c option, which displays a checking printout of the parsed form of the magic file

$ file -c cred.py 
cont    offset  type    opcode  mask    value   desc

This command provides a checking printout of the parsed form of the magic file associated with cred.py.

3.6. Using the -i Option

Now, let’s explore the -i option, which allows us to view the file’s MIME type.

For example, let’s check the MIME type information for a folder called test:

$ file -i test
test: inode/directory; charset=binary

The output of this command displays the MIME-type information for the test folder.

3.7. Using the -s Option

The -s option allows us to display information for special files.

For example, let’s check the information for a special file named /dev/sda using the sudo command:

$ sudo file -s /dev/sda

[sudo] password for kali: 
/dev/sda: DOS/MBR boot sector

The output indicates that the special file dev/sda is identified as a DOS/MBR boot sector.

3.8. Using the -z Option

We can use the -z option to look inside compressed files.

For example, let’s check the contents of a compressed file named google-cloud-cli-470.0.0-linux-x86_64.tar.gz:

$ file -z google-cloud-cli-470.0.0-linux-x86_64.tar.gz
google-cloud-cli-470.0.0-linux-x86_64.tar.gz: POSIX tar archive (gzip compressed data, was "google-cloud-cli-VERSION-linux-x86_64.tar", max compression)

The output reveals that the file google-cloud-cli-470.0.0-linux-x86_64.tar.gz is identified as a POSIX tar archive containing gzip compressed data. Additionally, it specifies that the original filename was google-cloud-cli-VERSION-linux-x86_64.tar and indicates the level of compression used.

4. Conclusion

In this article, we’ve explored the file command and its various options, which provide valuable insights into file types and formats.

Furthermore, understanding these functionalities allows us to efficiently use the file command to identify and manage files across various tasks in Linux environments.