Linux file Command Tutorial for Beginners (5 Examples)

While working on the Linux command line, there may be times when you'd encounter a file which is an archive (say a .zip file), but its extension (or l

Linux rev Command Tutorial for Beginners (with Examples)
How to Setup Apache Subversion with HTTPS Letsencrypt on CentOS 7
Linux more Command Tutorial for Beginners (5 Examples)

While working on the Linux command line, there may be times when you’d encounter a file which is an archive (say a .zip file), but its extension (or lack of it) would suggest otherwise. It’s in times like these, the file command proves helpful as it lets you determine the actual file type.

In this tutorial, we will discuss the basics of the file command using some easy to understand examples. But before we do that, it’s worth mentioning that all examples mentioned here have been tested on an Ubuntu 18.04 LTS machine.

Linux file command

As already mentioned in the beginning, the file command in Linux lets you see the type of file you’re dealing with. Following is its syntax:

file [OPTIONS] file1 file2 ....

And here’s how the tool’s man page explains it:

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

     The type printed will usually contain one of the words text (the file
     contains only printing characters and a few common control characters and
     is probably safe to read on an ASCII terminal), executable (the file con?
     tains the result of compiling a program in a form understandable to some
     UNIX kernel or another), or data meaning anything else (data is usually
     “binary” or non-printable).  Exceptions are well-known file formats (core
     files, tar archives) that are known to contain binary data.  When adding
     local definitions to /etc/magic, make sure to preserve these keywords.
     Users depend on knowing that all the readable files in a directory have
     the word “text” printed.  Don't do as Berkeley did and change “shell
     commands text” to “shell script”.

Following are some Q&A-styled examples that should give you a better idea on how the file command works.

Q1. How to use file command?

Simple, just execute ‘file’ with a file name as input. For example:

file test

Since ‘test’ is a directory on my system, the file command told me just that. Here’s the output it produced:

test: directory

Q2. How to test for multiple files?

This is pretty straight forward as well. Just pass the name of files as input, similar to the way we did in the previous example.

Here’s an example:

file test test.txt .local

And here’s the output this command produced:

test:     directory
test.txt: ASCII text
.local:   directory

Q3. How to make file show just the type in output?

As you can see in the file command output so far, the output contains two things: filename and its type. However, if you want, you can make file to just produce type in output. This can be done using the -b command line option.

For example, the following command:

file -b test

produced the following output on my system:

directory

Q4. How to see MIME type of files?

To view MIME type for a file, use the -i command line option. Here’s an example:

Q5. How to change the separator in output?

As we discussed above, in default mode, the file command produces in output a filename followed by its type. Both these details are separated by a colon (:). However, if you want, you can change this separator using the -F command line option.

For example:

file -F " +" test

produced the following output:

test + directory

Conclusion

To conclude, file is an important tool to have in your kitty. We’ve just scratched the surface here as the tool offers a lot more features in terms of command line options. Once you’re done with whatever we’ve discussed here, you can head to the file command man page to learn more about it.