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
- Open the terminal, or log in to your server using SSH. SSH is a safe way to control a faraway computer by typing.
- Type the command with a file name:
It prints the type, for example "ASCII text".file notes.txt - Check several files at once:
Each file gets one line of answer.file index.php logo.png backup.tar.gz - Check every file in the current folder:
The star means "everything here".file * - Show only the type, without the file name:
Thefile -b logo.png-boption means "brief". - Show the internet media type, also called MIME type:
A MIME type is a short label such asfile --mime-type logo.pngimage/pngthat programs use to know how to open a file. - Look inside a shortcut. A symbolic link is a shortcut to another file. Use:
Thefile -L shortcut-name-Loption follows the shortcut and describes the real file.
Reading the answer
| You may see | What it means |
|---|---|
| ASCII text | A plain text file |
| PNG image data | A picture in PNG format |
| gzip compressed data | A packed, smaller file |
| directory | A folder |
| PHP script | A 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 nametells you the real type of a file.- It checks the contents, not the file name.
- Use
-bfor a short answer and--mime-typefor the media type. - Use
file *to check a whole folder. - It only reads files. It never changes them.