Linux / UNIX: Determine File Type

How do I determine the file type under UNIX or Linux like operating systems?

You need to use the file command to determine file type. File command 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 on screen.


file Command Examples

Type the following command:
$ file /etc/passwd
Sample outputs:

/etc/passwd: ASCII text

Another example:
$ file /home/voffice/letter.doc
Sample outputs:

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

Find out type of the file.c, enter:
$ file file.c
Sample outputs:

file.c:   C program text

You can also use binary file, enter:
$ file $(which ls)
$ file /bin/ls
Sample outputs:

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

Finally, it can also display info about devices:
$ file /dev/sda{1,2,3}

/dev/sda1: block special (8/1)
/dev/sda2: block special (8/2)
/dev/sda3: block special (8/3)
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

3 comments… add one
  • sims6785 Jan 14, 2010 @ 4:00

    thanks a lot :)

  • iris Mar 2, 2011 @ 20:16

    This is useful!!

  • Mohit Malik Sep 19, 2012 @ 13:03

    very nice demonstration:) brief and accurate

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.