Instructions for using the file command
February 20, 2021
How to find out the type of a file without an extension or with the wrong extension
The file command can determine the file type for regular files and the file system used for disks.
The file utility uses magic bytes. The bottom line is that files of the same type have the same bytes in certain places in the file, the file program uses an extensive database in which the types of files are described and which sequences of bytes are located in which places. Thanks to this, using file utility, you can determine the type of files without an extension or with the wrong extension.
Usage is very simple - specify one or more file names, that is:
file FILE1 FILE2 FILE3 FILE4
To find out the type of a file without an extension named “file1”, run the command:
file1: JPEG image data, Exif standard: [TIFF image data, little-endian, direntries=11, manufacturer=samsung, model=SM-J710F, orientation=upper-left, xresolution=164, yresolution=172, resolutionunit=2, software=J710FXXU6CSE1, datetime=2019:11:10 16:31:24, GPS-Data], baseline, precision 8, 4128x3096, components 3
First there is the file name, and then information about it is shown separated by colons. In this example, it is JPEG, that is, an image. Also, meta information is partially shown for this file.
If you don't want the filename to be shown first, use the -b option:
file -b FILE
How to check the type of multiple files at once
As you can see in the screenshot, there are several files in the current folder without extensions. Let's find out the type of each of them.
You can specify the names of all files or use * (asterisk) to check all files in the current directory:
We managed to determine the type of each file:
- JPEG image
- Python script
- PDF document
- PE32 executable (console) Intel 80386 Mono/.Net assembly, for MS Windows - executable file for MS OS
- 7-zip archive
- Bourne-Again shell script - Bash script
- DOS/MBR boot sector, code offset 0x52+2, OEM-ID "NTFS - disk image with NTFS file system
- Microsoft Word 2007+ - Office Word Document
- OpenDocument Text - LibreOffice office document
How to find the extension by file type
To find out the correct extension for a specific file type, use the --extension option:
file --extension *
How to make sure that the access date of a file does not change when using file
The file program reads data from a file and therefore changes the date it was last accessed. The -p, --preserve-date option on systems that support utime or utimes will try to preserve the access time of the parsed files to pretend that the file has never been read.
How to find out the file system of a disk
For file system image files, the file program will show the file system type. For disks, this utility can also detect the file system type, but two additional options are required because the block device file is a special file, and sometimes it is just a link to a special file.
The -L, --dereference option forces symbolic links to be followed, like a similarly named option in ls (on systems that support symbolic links). This is the default if the POSIXLY_CORRECT environment variable is defined.
Typically, file utility only tries to read and determine the type of files passed as arguments, which stat reports as regular files. This prevents problems, as reading special files can have specific consequences. Specifying the -s, --special-files option causes the file to also read files that are special block or character files. This is useful for identifying the file system types of data in raw disk partitions, which are special block files. This option also causes the file to ignore the file size reported by stat, since on some systems it reports zero size for raw disk partitions.
The following information will display information about the file system on the /dev/sda drive:
file -s -L /dev/sda
/dev/sda: Linux rev 1.0 ext4 filesystem data, UUID=aad9d8d7-b1b9-435a-9e41-fc8159d2c484 (needs journal recovery) (extents) (64bit) (large files) (huge files)
As the output suggests, this is an ext4 file system. Some of its characteristics and supported features are also shown.
Without the -s option, that is, with the command
we would get the following result:
/dev/sda: block special (8/0)
How to look inside compressed files in file
With the -z, --uncompress option, you can try to look inside the compressed files. This option does not always work and sometimes it causes file utility to fail.