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.
file [option] [filename]
Example: Command displays the file type
file email.py file name.jpeg file Invoice.pdf file exam.ods file videosong.mp4
- -b, –brief : This is used to display just file type in brief mode.
file -b filename
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.
The output shows all files in the home directory
- directoryname/* option : This is used to display all files filetypes in particular directory.
The output shows all files in a particular directory.
- [range]* option: To display the file type of files in specific range.
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.
- -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.
file -f -
- -F option : File and file type are separated by :. But we can change separator using -F option
file -F "-" filename
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.
file -i filename
file -i input.txt file -i os.pdf
- -N option: Don’t pad filenames so that they align in the output.
file -N *
file -N *
- -s option: For special files
file -s filename
file /dev/sda file -s /dev/sda file /dev/sda5 file -s /dev/sda5
- filenames: Displays file types of multiple files
file input.txt .local Desktop
- -z option: Try to look inside compressed files.
file -z flash.tar.gz
- –help option: Print a help message and exit.
- ssh command in Linux with Examples
- cd command in Linux with Examples
- id command in Linux with examples
- tty command in Linux with examples
- ip command in Linux with examples
- top command in Linux with Examples
- pwd command in Linux with Examples
- dc command in Linux with examples
- du command in Linux with examples
- df command in Linux with Examples
- ln command in Linux with Examples
- ex command in Linux with examples
- ar command in Linux with examples
- env command in Linux with Examples
- ed command in Linux with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh