×

Search anything:

file command in Linux

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

In Linux, we use the file command to determine the type of file. With this command we can know the type of file we are dealing with, this also includes compressed files and special files. In this article, we learn about this command using various examples.

Table of contents.

  1. Introduction.
  2. Syntax,
  3. Command usage.
  4. Summary.
  5. References.

Introduction.

We use the file command in Linux to determine the type of file. We learn this command using various examples using commonly used options.

Syntax.

We write file commands using the following syntax.

$ file [OPTIONS] FILENAME]

file1

Commonly used command options with the file command are;

  • -b, to get a brief description of a file.
  • -c, to get a parsed for of a file.
  • -F, used to specify a string separator.
  • -i, to get the MIME type.
  • -z, to view details of compressed files.

Command usage.

  1. To list all file types in the file system, we use the * wild card character.
$ file *

file2

  1. To get a brief description of a file's description we use the -b option:
$ file -b [file name]
  1. we can also display types for multiple files by listing their file names as follows:
$ file [file1 fil2 file3]

file3

  1. To get a parsed description of a file we use the -c option:
$ file -c [file name]

file4

  1. To get information about file types in a particular directory we execute the command:
$ file [dir name]/*

The above command prints outs all files and their types in the directory.

  1. To display the MIME type of a file, we use the -i option:
$ file -i [file name]

file6

  1. Sometimes we may have compressed tar, zip, gzip files and want to know their contents without decompressing them. For this we can use the file command with the -z option:
$ file -z [compressed file]

file7

  1. We can also use regular expressions with the file command. For example to get the types of all files between f - m we write:
$ file [f - m]*

file8
Her ewe list all files whose names lie between the alphabet character f and m.

  1. The file and its type is separated by a colon by default, to change this we use the -F option followed by a custom separator. For example, if we want to separate files using a $ separator we write:
$ file -F "$" file

file9

  1. To read the contents of a special file we use the -s option:
$ file -s [special file]

file10

Summary.

As we have learned, we can know a file type using this command instead of deciphering this using only the file extension. We can also list the contents of a compressed file without decompressing a file.

We also learned how to use regex to determine file types within a range of files.

References.

For a comprehensive guide on how to use this command, we can check out its manual page by executing the command: $ man find.

file command in Linux
Share this