Linux/UNIX: Determine file type

by nixcraft [Last updated: March 5, 2006]

Use file command to determine file type. File tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic number tests, and language tests. The first test that succeeds causes the file type to be printed.

Examples

$ file /etc/passwd

/etc/passwd: ASCII text

$ file /home/voffice/letter.doc

/home/voffice/letter.doc: Microsoft Office Document

$ file file.c

file.c: C program text

$ file $(which ls)

/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.0, dynamically linked (uses shared libs), stripped

$ file /dev/sda{1,2,3}

/dev/sda1: block special (8/1)
/dev/sda2: block special (8/2)
/dev/sda3: block special (8/3)

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: How do I Create files needed to build and use DLLs under Linux?

Next post: Shell: How to wrap text files to fit specified width