Determine file type with the file command

by
on
October 8, 2008

The file command identifies the type or format of a file. It is a very handy command that can show you how to approach a file when the format is unknown. There are a variety of tests that it uses to determine its type, these tests include a filesystem test, magic number test, and language tests. The first successful test is the one that is printed to the user.

The magic number tests are used to check for files with data in particular fixed formats. To determine the format of a file in this manner, either a magic number identifier exists at the beginning of the file, or a specific identifier can be determined at a specific offset within the file. The file /usr/share/file/magic specifies what magic numbers are to be tested for, what message to print if a particular magic number is found, and additional information to extract from the file.

Here is an example with a directory which contains a variety of file types. The output lists the filename along with its type.

$ file *
data:        data
directory:   directory
emptyfile:   empty
program:     ELF 64-bit LSB executable, x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped
script.sh:   Bourne-Again shell script text executable
source.c:    ASCII C program text
symlink:     symbolic link to `program'
tarball.tar: POSIX tar archive (GNU)
tarball.tbz: bzip2 compressed data, block size = 900k
tarball.tgz: gzip compressed data, from Unix
text.txt:    ASCII text

This works equally well when the file names do not have have an extension, which is a very common case with Linux.

No Comments
commands
, ,

Related posts:

  1. Using tar to archive files
  2. Search for files with the find command
  3. Have a Bash script determine it’s own location
  4. Display the last part of a file with tail
  5. Convert PDF file to text with pdftotext

Comments (0)

No comments yet

Trackbacks (0)

No trackbacks yet

Leave a Comment

(displayed with your post)
(will not be published)
(optional)

Copyright 2008-2010 WiredRevolution.com. All rights reserved.