Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to use the Linux file command

Learn how to find out what kind of file something is, even when its name gives no clue.

VPS and Cloud Servers2 min read7 steps

What is the file command?

Linux is an operating system many servers use. You type commands into a window called the terminal. The file command looks inside a file and tells you what type it is. It does not trust the name. A file called photo.txt might really be a picture, and file will say so.

Think of it like checking what is inside a box instead of reading the label on the outside.

Steps

  1. Open the terminal, or log in to your server using SSH. SSH is a safe way to control a faraway computer by typing.
  2. Type the command with a file name:
    file notes.txt
    It prints the type, for example "ASCII text".
  3. Check several files at once:
    file index.php logo.png backup.tar.gz
    Each file gets one line of answer.
  4. Check every file in the current folder:
    file *
    The star means "everything here".
  5. Show only the type, without the file name:
    file -b logo.png
    The -b option means "brief".
  6. Show the internet media type, also called MIME type:
    file --mime-type logo.png
    A MIME type is a short label such as image/png that programs use to know how to open a file.
  7. Look inside a shortcut. A symbolic link is a shortcut to another file. Use:
    file -L shortcut-name
    The -L option follows the shortcut and describes the real file.

Reading the answer

You may seeWhat it means
ASCII textA plain text file
PNG image dataA picture in PNG format
gzip compressed dataA packed, smaller file
directoryA folder
PHP scriptA PHP program file
Tip: Use file on a download you are unsure about. If a "picture" is reported as an executable program, do not open it.

Quick recap

  • file name tells you the real type of a file.
  • It checks the contents, not the file name.
  • Use -b for a short answer and --mime-type for the media type.
  • Use file * to check a whole folder.
  • It only reads files. It never changes them.