Back to course

28. Determining File Type: The `file` Command

Linux Basics: From Zero to CLI Hero

Identifying the Unknown

In Linux, the file extension (.txt, .pdf, .jpg) is purely conventional and doesn't define the file type. The file command examines the contents of a file to determine its actual type (e.g., text, executable, compressed archive).

Basic Usage

bash $ file /bin/bash /bin/bash: 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

$ file /etc/hosts /etc/hosts: ASCII text

$ file my_photo.jpg my_photo.jpg: JPEG image data, JFIF standard 1.01

Practical Application

This is useful when dealing with downloaded files that have no extension or the wrong extension, allowing you to quickly determine if it's a script, a document, or an image before attempting to open or execute it.