file command in Linux with examples - GeeksforGeeks

19 Feb, 2021
file command is used to determine the type of a file. .file type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’). This command tests each argument in an attempt to categorize it.
It has three sets of tests as follows:
filesystem test: This test is based on the result which returns from a stat system call. The program verifies that if the file is empty, or if it’s some sort of special file. This test causes the file type to be printed.
magic test: These tests are used to check for files with data in particular fixed formats.
language test: This test search for particular strings which can appear anywhere in the first few blocks of a file.
Syntax:
file [option] [filename]

Example: Command displays the file type
file email.py
file name.jpeg
file Invoice.pdf
file exam.ods
file videosong.mp4

Options:
-b, –brief : This is used to display just file type in brief mode.Syntax:file -b filename
Example:file -b email.py
file -b input.txt
file -b os.pdf
Here, we can see that file type without filename.
Syntax:
file -b filename

Example:
file -b email.py
file -b input.txt
file -b os.pdf

Here, we can see that file type without filename.
* option : Command displays the all files’s file type.file *
The output shows all files in the home directory
file *

The output shows all files in the home directory
directoryname/* option : This is used to display all files filetypes in particular directory.Syntax:file directoryname/*
Example:file work/*
The output shows all files in a particular directory.
Syntax:
file directoryname/*

Example:
file work/*

The output shows all files in a particular directory.
[range]* option: To display the file type of files in specific range.Syntax:file [range]*
Example:file [a-z]*
file [a-e]*
The output shows the range of files.
Syntax:
file [range]*

Example:
file [a-z]*
file [a-e]*

The output shows the range of files.
-c option: Cause a checking printout of the parsed form of the magic file. This option is usually used in conjunction with the -m flag to debug a new magic file before installing it.file -c
Example:file -c

file -c

Example:
file -c

-f option: Read the names of the files to be examined from namefile (one per line) before the argument list. Either namefile or atleast one filename argument must be present; to test the standard input, use ‘-’ as a filename argument.Syntax:file -f -

Syntax:
file -f -

-F option : File and file type are separated by :. But we can change separator using -F optionSyntax:file -F "-" filename
Example:file -F - input.txt
file -F + os.pdf
The output shows file and file types are separated by – and +.
Syntax:
file -F "-" filename

Example:
file -F - input.txt
file -F + os.pdf

The output shows file and file types are separated by – and +.
-i option: To view mime type of file.Syntax:file -i filename
Example:file -i input.txt
file -i os.pdf

Syntax:
file -i filename

Example:
file -i input.txt
file -i os.pdf

-N option: Don’t pad filenames so that they align in the output.file -N *
Example:file -N *

file -N *

Example:
file -N *

-s option: For special filesSyntax:file -s filename
Example:file /dev/sda  
file -s /dev/sda  
file /dev/sda5  
file -s /dev/sda5 

Syntax:
file -s filename

Example:
file /dev/sda  
file -s /dev/sda  
file /dev/sda5  
file -s /dev/sda5 

filenames: Displays file types of multiple filesSyntax:file filenames
Example:file input.txt .local Desktop

Syntax:
file filenames

Example:
file input.txt .local Desktop

-z option: Try to look inside compressed files.Example:file -z flash.tar.gz

Example:
file -z flash.tar.gz

–help option: Print a help message and exit.
YouTubeGeeksforGeeks500K subscribersLinux Tutorials | file and wc commands | GeeksforGeeksWatch laterShareCopy link18/36InfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:35•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=A9411dSJYRk" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
shubham_singh
linux-command
Linux-file-commands
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
TCP Server-Client implementation in C
ZIP command in Linux with examples
tar command in Linux with examples
curl command in Linux with Examples
UDP Server-Client implementation in C
Conditional Statements | Shell Script
Cat command in Linux with examples
touch command in Linux with Examples
echo command in Linux with Examples
Compiling with g++