Linux File Command

Introduction

Before we start talking about linux file command, let's briefly understand-What is File Command ?

The file command in Linux displays the file type. It's useful when you need to identify a file type you've never seen before or when the file doesn't have a file extension.

By analyzing the file's contents, the file command helps users understand the file's properties and choose appropriate actions or applications for handling it.

In this tutorial, we will discuss the filecommand.

Linux File Command Syntax

The Linux file command has the following syntax:

file [OPTION] [FILE]

As parameters, it can take one or more file names.

How to Use the file Command to Find the File Type

The file command classifies files based on a sequence of tests, with the first successful test determining the file type.

When used without any options, the file command displays the file name and type in its most basic form:

file /etc/group
Output

/etc/group: ASCII text

Use the -b (--brief) option to show only the file type:

file -b /etc/group
Output

ASCII text

The /etc/group file is a text file, as you can see from the output above.

How to Find the File Type of Multiple Files

You can use the file command with several files:

file /bin/bash /opt/card.zip

Each file's type will be printed in a separate file by the command:

Output

/bin/bash:     ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=42602c973215ba5b8ab5159c527e72f38e83ee52, stripped
/opt/card.zip: Zip archive data, at least v1.0 to extract

Wildcard characters are also accepted. For instance, to determine the type of each .jpg file in the current directory, type:

Output

imgage001.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, progressive, precision 8, 2083x1250, components 3
imgage031.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, comment: "Created with GIMP", baseline, precision 8, 1280x1024, components 

How to View the Mime Type of a File

To determine the mime type of a file, use the -i (--mime) option:

file -i /var/www/index.html
Output

/var/www/index.html: text/html; charset=us-ascii

FAQs on Linux File Command

How do I use the file command in Linux?

To use the file command, simply provide the path to the file as an argument. For example: file myfile.txt.

What information does the file command provide?

The file command provides information such as the file type, file format, encoding, and other characteristics based on the analysis of the file's content.

What does the file type "ASCII text" mean in the file command output?

"ASCII text" indicates that the file contains only plain text characters encoded using ASCII (American Standard Code for Information Interchange).

Can the file command recognize various archive formats like zip or tar?

Yes, the file command can recognize many common archive formats like ZIP, TAR, GZIP, and more, and provides relevant information about them.

How does the file command determine the file type?

The file command determines the file type by examining the file's magic number, which is a unique identifier present in the file's header. It compares this number against a pre-defined database of magic numbers to find a match.

What is the significance of the file extension in the file command output?

The file extension has limited significance for the file command. It may be used as a clue, but the command primarily relies on the analysis of the file's content rather than the extension.

Can the file command recognize encrypted or compressed files?

Yes, the file command can often recognize encrypted or compressed files based on their magic numbers and provide information about the encryption or compression algorithm used.

Conclusion

You should now be able to use the Linux file command with confidence. See the file man page for further information on the find command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.