Lesser known Linux Commands: file

file: file is a command that will identify the type of file – executable, text or data. If the file is one of well-known formats like zip, tar, pdf then it says so.

eg. $ file *

tamil-keyboard-color.jpg:           JPEG image data, JFIF standard 1.01
tamil-keyboard-unicode.png:         PNG image, 815 x 436, 8-bit/color RGB, non-interlaced
targets:                            ASCII text
test_data:                          directory
testprint.txt:                      ASCII text
test.xls:                           Microsoft Office Document
thinclient_lts.conf:                ASCII English text
ttf_fonts:                          directory
upgradehims.odt:                    OpenDocument Text
vec:                                directory
w2008.sxw:                          OpenOffice.org 1.x Writer document
w2009.odt:                          OpenDocument Text

file looks into content and don’t determine based on file extension. Normally every type of file like pdf, odt have some specific signature at specific offset – called magic. file looks for this magic and determines nature of file. file can also determine the programming language in case of programmes like – shell script, perl script based on hashbang #!. The magic number details are stored in /usr/share/file/magic (in debian)

file is very useful in several situations. Normally whenever there is a filesystem  corruption and fsck is run, many files are recovered and kept under lost+found directory. These recovered files will have names which are normally the inode numbers and not the original name. Thus no extensions or names to guess what it contains. Using file command on such file will give us the type of file. Probably we can rename with appropriate extension.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: