How to Use File Command in Linux: Syntax and Examples
Use the
file command in Linux to determine the type of a file by analyzing its content, not just its extension. Run file filename to see the file type information.Syntax
The basic syntax of the file command is:
file [options] filename
Here, filename is the name or path of the file you want to check. Options can modify the output or behavior.
bash
file filename
Example
This example shows how to use the file command to identify the type of a file named example.txt and a binary file /bin/ls.
bash
file example.txt file /bin/ls
Output
example.txt: ASCII text
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=..., stripped
Common Pitfalls
Common mistakes when using file include:
- Relying on file extensions instead of using
fileto check the actual content. - Using
fileon directories without the-soption, which may not give detailed info. - Not having permission to read the file, causing incomplete or no output.
Always ensure you have read access and use file -s for special files like devices or partitions.
bash
file myfile.pdf file -s /dev/sda1
Output
myfile.pdf: PDF document, version 1.4
/dev/sda1: Linux rev 1.0 ext4 filesystem data, UUID=..., volume name "root"
Quick Reference
| Option | Description |
|---|---|
| -b | Brief output, omit filename |
| -i | Output MIME type string |
| -s | Read special files like devices |
| -L | Follow symbolic links |
| --help | Show help message |
Key Takeaways
Use
file filename to identify the actual file type by content.File extensions can be misleading;
file reads file data to determine type.Use options like
-i for MIME type or -s for special files.Ensure you have read permission to get accurate results.
The
file command works on any file type including text, binaries, and devices.