file command in Linux
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
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.
- Introduction.
- Syntax,
- Command usage.
- Summary.
- 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]
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.
- To list all file types in the file system, we use the * wild card character.
$ file *
- To get a brief description of a file's description we use the -b option:
$ file -b [file name]
- we can also display types for multiple files by listing their file names as follows:
$ file [file1 fil2 file3]
- To get a parsed description of a file we use the -c option:
$ file -c [file name]
- 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.
- To display the MIME type of a file, we use the -i option:
$ file -i [file name]
- 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]
- 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]*
Her ewe list all files whose names lie between the alphabet character f and m.
- 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 "__aSyNcId_<_lfxcW_tt__quot; file
- To read the contents of a special file we use the -s option:
$ file -s [special file]
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.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.