# file — Determine File Type by Content

> Detect the real file type by content instead of extension with file — via magic-number patterns, including MIME type, encoding and script use.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/file/

<!-- PROSE:intro -->
`file` determines a file's type not from its extension but from its actual content: it reads characteristic byte sequences at the start of the file – the so-called magic numbers – and matches them against a pattern database. This lets you reliably identify even misnamed or unnamed files, such as a PNG hiding behind a `.dat` extension or an executable ELF binary with no extension at all. For scripts, `--mime-type` emits a clean MIME type, `-i` adds the encoding, and `-b` omits the filename. That makes `file` the tool of choice before you open an unknown file or process an upload.
<!-- PROSE:intro:end -->

## Basic Usage

`file <file>` — Identify the type of a file.

```bash
file document.pdf
```

`file <file1> <file2>` — Identify multiple files at once.

```bash
file *.bin
```

`file -b <file>` — Brief mode: omit the filename from output.

```bash
file -b image.png
```

`file *` — Identify all files in the current directory.

```bash
file /usr/bin/*
```

## MIME Types

`file -i <file>` — Show MIME type and encoding.

```bash
file -i script.py
```

`file --mime-type <file>` — Show only the MIME type.

```bash
file --mime-type photo.jpg
```

`file --mime-encoding <file>` — Show only the MIME encoding (charset).

```bash
file --mime-encoding data.csv
```

`file -b --mime-type <file>` — Show MIME type only, no filename (useful in scripts).

```bash
file -b --mime-type upload.dat
```

## Options

`file -z <file>` — Look inside compressed files.

```bash
file -z archive.gz
```

`file -L <symlink>` — Follow symbolic links.

```bash
file -L /usr/bin/python
```

`file -s <device>` — Read block/character special files (devices).

```bash
sudo file -s /dev/sda1
```

`file -f <list>` — Read filenames from a list file (one per line).

```bash
file -f filelist.txt
```

`file -k <file>` — Keep going: show all matching types, not just the first.

```bash
file -k mystery.bin
```

## Special Input

`file -` — Read from stdin and identify the content.

```bash
curl -s https://example.com | file -
```

`echo '<data>' | file -` — Identify piped data.

```bash
echo 'PK' | file -
```

`file -b - < <file>` — Identify redirected input (brief mode).

```bash
file -b - < unknown.dat
```

## Common Patterns

`find . -type f -exec file {} + | grep 'text'` — Find all text files recursively.

```bash
find /var/log -type f -exec file {} + | grep 'text'
```

`find . -type f -exec file {} + | grep 'ELF'` — Find all binary executables.

```bash
find /usr/bin -type f -exec file {} + | grep 'ELF'
```

`file -i * | grep 'charset=utf-8'` — Find all UTF-8 encoded files.

```bash
file -i *.txt | grep 'charset=utf-8'
```

`file * | grep -i image` — Find all image files regardless of extension.

```bash
file uploads/* | grep -i image
```

`for f in *; do echo "$f: $(file -b "$f")"; done` — Custom formatted file type listing.

```bash
for f in downloads/*; do echo "$f: $(file -b "$f")"; done
```

<!-- PROSE:outro -->
## Conclusion

`file` is the honest counterpart to the file extension: it looks at the content instead of the name and tells you what a file really is. For everyday use a bare `file name` is often enough; for scripts and pipelines `-b --mime-type` gives you a clean, machine-readable MIME type with no noise. Combined with `find -exec` you sweep entire directory trees for text, ELF binaries or images – regardless of how the files are named. Keep in mind, though: `file` guesses via heuristics and can be fooled, especially on very small or empty files. For pure metadata reach for `stat`, for a directory overview reach for `ls`, and to locate the files reach for `find`.

## Further Reading

- [file man page (man7.org)](https://man7.org/linux/man-pages/man1/file.1.html) – complete reference for every option and exit status
- [Wikipedia: file (command)](https://en.wikipedia.org/wiki/File_(command)) – background on the command and the magic-number concept
<!-- PROSE:outro:end -->

## Related Commands

- [stat](https://www.jpkc.com/db/en/cheatsheets/files-text/stat/) – show complete file metadata
- [ls](https://www.jpkc.com/db/en/cheatsheets/files-text/ls/) – list directory contents
- [find](https://www.jpkc.com/db/en/cheatsheets/files-text/find/) – search files recursively, often combined with file

