Day 13 - file

Writer: Rauli Kaksonen

There are two basic commands I ran whenever I have an unknown file and I want to learn more about it. The commands are: file and strings.

Today's topic is file. It is a tool for identifying the type of a file based on the file content. The tool contains a database of fingerprints, which it uses to recognizing the file type.

The file tool was originally written by Ian F. Darwin and first commit of the tool was made back in 1987. There have been multiple derivatives of the tool and nowadays there are also other contributors.

I use file tool on daily basis to resolve the types of the unknown files I encounter. I also have used it in scripting to identify file type and decide next actions based on the file type.

For manual operation, the file tool can be invoked just by providing the name of the file to identify, e.g. like this:

$ file /bin/bash 
/bin/bash: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=12f73d7a8e226c663034529c8dd20efec22dde54, stripped

For automation, I use combination of arguments -b --mime-type to get the mime type of the file and nothing else for easy scripting:

$ file -b --mime-type /bin/bash 
application/x-sharedlib

The file tool comes with more options, available the usual way:

$ file -h

Have fun!

ps. Please note that some distribution may have different variant of the file tool.

Links:

  • http://darwinsys.com/file/

  • https://en.wikipedia.org/wiki/File_(command)