How to use file comand.

How To Use File Comand.

The file command displays the type of a file.


Linux File Command Syntax

The syntax for the Linux file command is as follows:

file [OPTION] [FILE]

It can take one or more file names as its arguments.


How to Use the file Command to Find the File Type

The file command classifies files based on a series of tests and determines the file type based on the first successful test.

In its simplest form when used without any option, the file command will display the file name along with the file type:

file /etc/group
/etc/group: ASCII text

To show just the file type use the -b (–brief) option:

file -b /etc/group
ASCII text

As you can see from the output above the /etc/group file is a text file.


How to Find the File Type of Multiple Files

You can pass more than one files to the file command:

file /bin/bash /opt/

The command will print the type of each file on a separate file:

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

It also accepts wildcard characters. For example, to find the type of each .jpg files in the current directory you would run:

file *.jpg
imgage001.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, progressive, precision 8, 2083x1250, components 3
mgage031.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

Use the -i (–mime) option to determine the mime type of a file:

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



By now you should have a good understanding of how to use the Linux file command.