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.


Recommended Posts:
- 'IPCS' command in Linux with examples
- select command in Linux with examples
- Sed Command in Linux/Unix with examples
- ZIP command in Linux with examples
- SORT command in Linux/Unix with examples
- Cat command in Linux with examples
- Head command in Linux with examples
- Tail command in Linux with examples
- wc command in Linux with examples
- tar command in Linux with examples
- atrm command in Linux with examples
- bc command in Linux with examples
- AWK command in Unix/Linux with examples
- tr command in Unix/Linux with examples
- mv command in Linux with examples
- Paste command in Linux with examples
- comm command in Linux with examples
- cmp Command in Linux with examples
- cut command in Linux with examples
- cp 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 contribute@geeksforgeeks.org. 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

