The Linux file command is used to determine the type of a file. To achieve this it analyzes the header, magic number and the contents of the file. Other operating systems like Windows use a file extension to determine the file type (i.e. exe, doc, txt, etc.). In Linux files are not required to have an extension and using the file command is an efficient way to determine type of a file.

Basic Usage of File Command

The basic usage of the file command is very simple. You simply invoke the command followed by the name of the file you want to analyze.

file [file1]

Here is an example:

$ file linux.words 
 linux.words: ASCII text

The output generated by the file command is the file name followed by it's type.

It is also possible to analyze several files simultaneously. To do this, simple list them separated by a blank space.

file [file1] [file2] [file3]

Here is an example of analyzing multiple files at once.

$ file dict.gif countdown.sh harrison-bergeron.txt
 dict.gif:              GIF image data, version 89a, 1150 x 632
 countdown.sh:          Bourne-Again shell script, ASCII text executable
 harrison-bergeron.txt: ASCII text

You can also use a wildcard to analyze all of the files of a directory.

$ file Desktop/*
 Desktop/articles.txt: ASCII text
 Desktop/TEMP:                directory
 Desktop/Untitled Document 1: VISX image file

Follow Symlinks to Analyze File

By default the file command will not follow a symbolic link, it will merely tell you that the argument your provided is a link. Here is an example:

$ file /etc/redhat-release 
 /etc/redhat-release: symbolic link to fedora-release

You can provide the -L (--dereference) option to follow the symlink and analyze the file it is pointed to.

$ file -L /etc/redhat-release 
 /etc/redhat-release: ASCII text

NOTE: Please note that the output still shows the name of the link instead of the referenced file.

Provide List to be Analyzed from a File

You can provide a file containing a list of files to be analyzed by using the -f (--file-from) option. The list should be filenames, one per line.

Here is an example file containing a list of filenames which we will pass to the file command.

$ cat test.txt 
 lynx.cfg
 dict.gif
 harrison-bergeron.txt

We can pass this as an argument after invoking the file command with the -f option, like so:

$ file -f test.txt 
 lynx.cfg:              ASCII text
 dict.gif:              GIF image data, version 89a, 1150 x 632
 harrison-bergeron.txt: ASCII text

Show MIME Type Strings

Using the -i (--mime) option with the file command will output the mime type along with the encoding used by the file. This is ideal for correctly identifying text files.

$ file -i testfile
 testfile: text/plain; charset=us-ascii

Formatting the File Command Output

You can change the output by using the available formatting options outlined below.

Do Not Show File Names

Using the -b (--brief) option will not print the filenames to output lines. This can be useful when scripting, but might be confusing if you are analyzing several files.

$ file -b test.txt 
 ASCII text

Specify Separator Between Filename and Results

By default the file command uses a colon between the filename and it's results in the output.

$ file test.txt 
 test.txt: ASCII text

You can change that separator to anything you wish using the -F (--separator) option. For example, let's say we wanted to use a dash instead.

$ file -F- test.txt 
 test.txt- ASCII text

You can use single quotes to wrap special characters. For example, if we wanted to use a close parenthesis.

$ file -F')' test.txt 
 test.txt) ASCII text

We can even use some ascii art to make an arrow the separator.

$ file -F  »»---------------------► test.txt 
 test.txt »»---------------------► ASCII text

Do Not Pad Results in Output

By default file will pad the results in output to make them all align neatly and easier to read.

$ file dict.gif countdown.sh harrison-bergeron.txt
 dict.gif:              GIF image data, version 89a, 1150 x 632
 countdown.sh:          Bourne-Again shell script, ASCII text executable
 harrison-bergeron.txt: ASCII text

Using the -N (--no-pad) option you can disable this feature like so:

$ file -N dict.gif countdown.sh harrison-bergeron.txt
 dict.gif: GIF image data, version 89a, 1150 x 632
 countdown.sh: Bourne-Again shell script, ASCII text executable
 harrison-bergeron.txt: ASCII text

Conclusion

In this article we discussed using the file command to analyze files to find their types. We also discussed some of it's options and how to format the output.

Resources and Links