The file command is used to determine the type of a file based on its contents, rather than its extension. It can identify various file types including text files, executable binaries, and more.
-
Syntax:
file [options] [file...]
-
Examples:
file example.txt
- Displays the file type of
example.txt.
file -i example.txt
- Displays the MIME type of
example.txt.
file -b example.txt
- Displays the file type without the filename.
file -f filelist.txt
- Reads filenames from
filelist.txtand displays their types.
- Displays the file type of
-
-bor--brief:- Displays only the file type, omitting the filename.
file -b example.txt
- Outputs just "ASCII text" instead of "example.txt: ASCII text".
-
-ior--mime:- Displays the MIME type of the file.
file -i example.txt
- Outputs
text/plain; charset=us-asciifor a text file.
-
-f [file]or--files-from=[file]:- Reads a list of filenames from the specified file and displays their types.
file -f filelist.txt
- Outputs the types of all files listed in
filelist.txt.
-
-zor--uncompress:- Tries to uncompress files before determining their type.
file -z compressed_file.gz
- Identifies the type of a compressed file after decompression.
-
--help:- Displays help information about the
filecommand.
file --help
- Shows usage information and available options.
- Displays help information about the
| File Type | Description |
|---|---|
| ASCII text | Plain text file |
| HTML document | File containing HTML markup |
| JPEG image | JPEG image file |
| gzip compressed | Gzip compressed file |
| ELF 64-bit LSB executable | 64-bit Linux executable file |
| PDF document | Portable Document Format file |
| directory | Directory or folder |
-
Determining File Type:
- Use
fileto get detailed information about a file's type.
file example.zip
- Outputs something like
Zip archive data, at least v2.0 to extract.
- Use
-
Handling Multiple Files:
- You can specify multiple files to check their types in a single command.
file file1.txt file2.png file3.zip
- Outputs the type for each file listed.
-
Using with Pipes:
- Use
filein combination with other commands to analyze file types of output.
ls | xargs file- Identifies the types of files listed by
ls.
- Use
The file command is a useful tool for identifying the type of a file based on its content rather than its extension. It provides a straightforward way to determine whether a file is a text document, an image, an executable, or any other type. By using various options, you can customize the output to fit your needs and handle multiple files efficiently.