Linux and Unix file command tutorial with examples
Tutorial on using file, a UNIX and Linux command for determining file types. Examples of a single file, multiple files, viewing mime types and compressed files.
What is the file command in UNIX? ¶
file command determines the file type of a file. It reports the file type
in human readable format (e.g. ‘ASCII text’) or MIME type (e.g. ’text/plain;
charset=us-ascii’). As filenames in UNIX can be entirely independent of file
file can be a useful command to determine how to view or work with a
How to determine the file type of a file ¶
To determine the file type of a file pass the name of a file to the
file command.The file name along with the file type will be printed to
file file.txt file.txt: ASCII text
To show just the file type pass the
file -b file.txt ASCII text
file command can be useful as filenames in UNIX bear no relation to their
file type. So a file called
somefile.csv could actually be a zip file. This
can be verified by the
file somefile.csv somefile.csv: Zip archive data, at least v2.0 to extract
How to determine the file type of multiple files ¶
file command can also operate on multiple files and will output a separate
line to standard output for each file.
file unix-*.md unix-cat.md: ASCII text, with very long lines unix-comm.md: ASCII text, with very long lines unix-cut.md: UTF-8 Unicode text unix-exit-status.md: ASCII text unix-file.md: ASCII text, with very long lines
How to view the mime type of a file ¶
To view the mime type of a file rather than the human readable format pass the
file -i file.txt file.txt: text/plain; charset=us-ascii
This can be combined with the
-b option to just show the mime type.
file -i -b file.txt text/plain; charset=us-ascii
How to view compressed files without decompressing ¶
To view compressed files without decompressing them pass the
-z option. In the
following example a file
foo.txt.gz is a gzip compressed ASCII text file.
file -z bar.txt.gz bar.txt.gz: ASCII text (gzip compressed data, was "bar.txt", last modified: Wed Sep 7 19:31:23 2016, from Unix)
Further reading ¶
Can you help make this article better? You can edit it here and send me a pull request.
Linux and Unix ps command tutorial with examples
Tutorial on using ps, a UNIX and Linux command for reporting information on running processes. Examples of searching by user, group, executable name and killing processes.
Linux and Unix ping command tutorial with examples
Tutorial on using ping, a UNIX and Linux command for sending ICMP ECHO_REQUEST packets to network hosts. Examples of checking if a remote host is up and limiting to IPv4 and IPv6 requests.
Linux and Unix fmt command tutorial with examples
Tutorial on using fmt, a UNIX and Linux command for formatting text. Examples of formatting a file, setting the column with and formatting uniform spaces.