5 UNIX / Linux file Command Examples

What is file?
5 file examples
Syntax and Options
Related Commands

What is file?

The file command accepts a file as an argument and performs certain tests to determine the correct file type. For example text, executable etc.

5 file Examples

1. A basic example

The following basic example determines the file type for all the files present in current directory.

$ file *
cmd:          ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xfac3f75dfae01e0ac037c39099182eb5c51fca82, not stripped
cmd.c:        ASCII English text
file:         ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xcd78d8859677bf20f34fa16142593494b01cd1ee, not stripped
file.c:       ASCII C program text
helloworld.c: ASCII text
helloworld.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
input.txt:    ASCII text
split:        directory

2. Change the delimiter using -F option

As can be seen in the above example, the default delimiter between file name and file type is ‘:’. This can be changed using -F option.

Here is an example :

$ file -F - *
cmd-          ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xfac3f75dfae01e0ac037c39099182eb5c51fca82, not stripped
cmd.c-        ASCII English text
file-         ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xcd78d8859677bf20f34fa16142593494b01cd1ee, not stripped
file.c-       ASCII C program text
helloworld.c- ASCII text
helloworld.o- ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
input.txt-    ASCII text
split-        directory

3. Determine the file type of stdin using – option

To determine the file type of stdin file, use the ‘-‘ option.

Here is an example :

$ file  -
Hello World
/dev/stdin: ASCII text

4. Represent file type in form of mime-type using -i option

By default, the information related to file type is presented in form of human readable string. This can be changed to mime-type string using -i option.

Here is an example:

$ file -i *
cmd:          application/x-executable; charset=binary
cmd.c:        text/plain; charset=us-ascii
file:         application/x-executable; charset=binary
file.c:       text/x-c; charset=us-ascii
helloworld.c: text/plain; charset=us-ascii
helloworld.o: application/x-object; charset=binary
input.txt:    text/plain; charset=us-ascii
split:        inode/directory; charset=binary

5. Change alignment of output using -N option

The alignment of the output can be changed in way that there is no padding after file name by using -N option.

Here is an example :

$ file -N *
cmd: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xfac3f75dfae01e0ac037c39099182eb5c51fca82, not stripped
cmd.c: ASCII English text
file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xcd78d8859677bf20f34fa16142593494b01cd1ee, not stripped
file.c: ASCII C program text
helloworld.c: ASCII text
helloworld.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
input.txt: ASCII text
split: directory

Syntax and Options

     file [-bchiklLNnprsvz0] [--apple] [--mime-encoding] [--mime-type]
          [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...
     file -C [-m magicfiles]
     file [--help]
Short Option Long Option Option Description
-b –brief Do not prepend filenames to output lines (brief mode).
-C –compile Write a magic.mgc output file that contains a pre-parsed version of the magic file or directory.
-c –checking-printout Cause a checking printout of the parsed form of the magic file. This is usually used in conjunction with the -m flag to debug a new magic file before installing it.
-e –exclude Exclude the test named in testname from the list of tests made to determine the file type
-F –separator Use the specified string as the separator between the filename and the file result returned. Defaults to ‘:’.
-h –no-dereference option causes symlinks not to be followed (on systems that support symbolic links). This is the default if the environment variable POSIXLY_CORRECT is not defined.
-i –mime Causes the file command to output mime type strings rather than the more traditional human readable ones. Thus it may say ‘text/plain; charset=us-ascii’ rather than “ASCII text”.
-k –keep-going Don’t stop at the first match, keep going. Subsequent matches will be have the string ‘12- ’ prepended. (If you want a new-line, see the -r option.)

Related Commands

magic
hexdump
od
strings

Comments on this entry are closed.