file command is used to determine the type of a file. .file type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’). This command tests each argument in an attempt to categorize it.
It has three sets of tests as follows:
- filesystem test: This test is based on the result which returns from a stat system call. The program verifies that if the file is empty, or if it’s some sort of special file. This test causes the file type to be printed.
- magic test: These tests are used to check for files with data in particular fixed formats.
- language test: This test search for particular strings which can appear anywhere in the first few blocks of a file.
Syntax:
file [option] [filename]
Example: Command displays the file type
file email.py file name.jpeg file Invoice.pdf file exam.ods file videosong.mp4
Options:
-
-b, –brief : This is used to display just file type in brief mode.
Syntax:
file -b filename
Example:
file -b email.py file -b input.txt file -b os.pdf
Here, we can see that file type without filename.
-
* option : Command displays the all files’s file type.
file *
The output shows all files in the home directory
-
directoryname/* option : This is used to display all files filetypes in particular directory.
Syntax:
file directoryname/*
Example:
file work/*
The output shows all files in a particular directory.
-
[range]* option: To display the file type of files in specific range.
Syntax:
file [range]*
Example:
file [a-z]* file [a-e]*
The output shows the range of files.
-
-c option: Cause a checking printout of the parsed form of the magic file. This option is usually used in conjunction with the -m flag to debug a new magic file before installing it.
file -c
Example:
file -c
-
-f option: Read the names of the files to be examined from namefile (one per line) before the argument list. Either namefile or atleast one filename argument must be present; to test the standard input, use ‘-’ as a filename argument.
Syntax:
file -f -
-
-F option : File and file type are separated by :. But we can change separator using -F option
Syntax:
file -F "-" filename
Example:
file -F - input.txt file -F + os.pdf
The output shows file and file types are separated by – and +.
-
-i option: To view mime type of file.
Syntax:
file -i filename
Example:
file -i input.txt file -i os.pdf
-
-N option: Don’t pad filenames so that they align in the output.
file -N *
Example:
file -N *
-
-s option: For special files
Syntax:
file -s filename
Example:
file /dev/sda file -s /dev/sda file /dev/sda5 file -s /dev/sda5
-
filenames: Displays file types of multiple files
Syntax:
file filenames
Example:
file input.txt .local Desktop
-
-z option: Try to look inside compressed files.
Example:
file -z flash.tar.gz
-
–help option: Print a help message and exit.