How To Check File Type In Linux – Determine File Format Using Magic

Identifying the format of a file begins with knowing how to check file type in Linux using commands like `file`. This is one of the first skills you need when working with the command line, especially if you deal with unknown or misnamed files. In this guide, you will learn several methods to determine file types quickly and accurately, from basic commands to advanced techniques.

Linux does not rely on file extensions like Windows does. A file named `data.txt` could actually be a binary executable or a compressed archive. The system looks at the file’s magic numbers and headers to determine its real type. This makes the `file` command your best friend.

By the end of this article, you will know how to check file types for single files, multiple files, directories, and even files without extensions. You will also learn about related tools like `stat`, `ls`, and `mimetype`. Let’s get started.

How To Check File Type In Linux

The most common and reliable way to check a file’s type is the `file` command. It reads the file’s magic bytes and compares them against a database of known signatures. This works even if the file has no extension or a wrong one.

Here is the basic syntax:

file [options] filename

For example, to check a file called `example.pdf`, you would run:

file example.pdf

The output will tell you the exact type, like “PDF document, version 1.4”. If the file is a text file, it will say “ASCII text” or “UTF-8 Unicode text”.

Using The File Command With Multiple Files

You can check several files at once by listing them all:

file file1.txt file2.jpg file3.tar.gz

Or use wildcards to check all files in a directory:

file *

This will show the type of every file in the current folder. It is very handy for scanning unknown directories.

Common Options For The File Command

The `file` command has several useful options:

  • -b or --brief: Shows only the file type, not the filename.
  • -i or --mime: Displays MIME type instead of a human-readable description.
  • -s or --special-files: Reads special files like block devices or sockets.
  • -L or --dereference: Follows symbolic links and shows the target’s type.

Example with the `-i` option:

file -i example.pdf

Output: example.pdf: application/pdf; charset=binary

This is usefull when you need MIME types for web applications or scripts.

Checking File Type Without The File Command

Sometimes you may not have the `file` command installed, or you want a quick alternative. Here are other ways to determine file types.

Using The Stat Command

The `stat` command shows detailed information about a file, including its type. Run:

stat filename

Look for the “File:” line in the output. It will show something like “regular file” or “directory”. This is not as detailed as `file`, but it confirms the basic type.

Using The Ls Command With -L Option

The `ls -l` command shows file permissions, owner, size, and the first character indicates the type:

  • - for regular file
  • d for directory
  • l for symbolic link
  • c for character device
  • b for block device
  • s for socket
  • p for named pipe

Example:

ls -l myfile.txt

If the output starts with -rw-r--r--, it is a regular file. If it starts with d, it is a directory.

Checking File Extension With The Basename Command

While extensions are not reliable, you can still extract them using:

basename filename .txt

Or use parameter expansion:

echo ${filename##*.}

This gives you the extension, but remember it may be misleading. A file named `script.sh` could be a shell script or a binary.

Determining File Type For Directories And Special Files

Directories and special files are also checked with the `file` command. For example:

file /home/user

Output: /home/user: directory

For symbolic links, use the `-L` option to see the target’s type:

file -L linkname

Without `-L`, it will show “symbolic link to …”.

Checking File Type In Scripts

You can use `file` inside shell scripts to make decisions. For example:

if file "$1" | grep -q "ASCII text"; then
    echo "This is a text file."
fi

This checks if the file is a text file before processing it.

Using The Mime Type Command

Some Linux distributions have the `mimetype` command, which is part of the `shared-mime-info` package. It works similarly to `file -i`:

mimetype filename

Output: filename: application/pdf

This is usefull for desktop environments and file managers.

Checking File Type For Compressed Archives

Compressed files often have multiple layers. The `file` command can show the inner type as well. For example:

file archive.tar.gz

Output: archive.tar.gz: gzip compressed data, from Unix

If you want to see the type inside the archive, you can use `tar` or `unzip` commands first.

Using The File Command With No Extension Files

Many files in Linux have no extension. The `file` command handles them perfectly. For instance:

file README

Output: README: ASCII text

This is why `file` is essential for system administration and forensics.

Common File Types And Their Magic Numbers

Magic numbers are the first few bytes of a file that identify its type. Here are some common ones:

  • %PDF – PDF file
  • PK – ZIP archive or Office document
  • GIF8 – GIF image
  • ‰PNG – PNG image
  • MZ – Windows executable
  • #! – Script (shebang)

You can view these with the `xxd` or `hexdump` command:

xxd -l 4 filename

This shows the first 4 bytes in hexadecimal.

Installing The File Command

Most Linux distributions come with `file` pre-installed. If it is missing, install it using your package manager:

  • Debian/Ubuntu: sudo apt install file
  • Red Hat/CentOS: sudo yum install file
  • Arch Linux: sudo pacman -S file

Once installed, you can start using it immediately.

Practical Examples Of Checking File Types

Let’s go through some real-world scenarios.

Scenario 1: Unknown File From Email

You download an attachment named `invoice`. It has no extension. Run:

file invoice

If it says “PDF document”, you can open it with a PDF viewer. If it says “HTML document”, it is a web page.

Scenario 2: Multiple Files In A Folder

You have a folder with 100 files and need to sort them. Use:

file * | sort

This lists all files grouped by type.

Scenario 3: Checking A Script

You have a file called `run.sh`. Run:

file run.sh

If it says “Bourne-Again shell script”, it is a bash script. If it says “ELF 64-bit LSB executable”, it is a compiled binary.

Limitations Of The File Command

The `file` command is not perfect. It relies on a database of magic numbers, which may be outdated. Also, some files can have misleading headers. For example, a file may start with PDF magic bytes but be corrupted. Always verify with other tools if needed.

Another limitation is that `file` cannot always distinguish between similar types, like different versions of the same format.

Using The Head Command For Quick Checks

You can use `head` to view the first few lines of a file. For text files, this often reveals the type:

head -n 5 filename

If you see “”, it is an HTML file. If you see “#!/bin/bash”, it is a script.

For binary files, `head` will show gibberish, indicating it is not text.

Using The Hexdump Command For Deep Inspection

For advanced users, `hexdump` or `xxd` can show the raw bytes:

xxd filename | head

This is usefull when you need to identify custom or rare file formats.

Automating File Type Checks

You can create a script to check all files in a directory and log the results:

#!/bin/bash
for file in /path/to/dir/*; do
    file "$file" >> filetypes.log
done

This saves the types to a log file for later review.

Frequently Asked Questions

What is the difference between `file` and `stat`?

`file` shows the content type based on magic numbers, while `stat` shows metadata like permissions and size. `file` is more accurate for determining the actual format.

Can I check file type without any command?

Yes, you can use a file manager like Nautilus or Dolphin, which shows file types in the properties window. But the command line is faster for bulk checks.

Why does `file` sometimes say “data”?

If the file does not match any known magic numbers, `file` returns “data”. This means the format is unknown or generic.

How do I check file type for a symbolic link?

Use `file -L linkname` to follow the link and show the target’s type. Without `-L`, it shows the link itself.

Is there a GUI tool for checking file types?

Most file managers show file types in the properties dialog. You can also use `gnome-file-types` or `kde-file-types` for more details.

Conclusion

Knowing how to check file type in Linux is a fundamental skill that saves time and prevents errors. The `file` command is your primary tool, but alternatives like `stat`, `ls`, and `mimetype` also work well. Practice with different files to get comfortable with the output.

Remember that file extensions are not reliable in Linux. Always use the `file` command to verify the true format. This is especially important when handling unknown files from the internet or legacy systems.

With the techniques in this guide, you can confidently identify any file type in Linux. Start using the `file` command today and make your workflow more efficient.