File command in Linux used to determine the type of a file. Generally users are assume file type by looking at extension of file. For example when you see a file with an extension of gif, jpg, bmp, or png you think of an image file. File command helps you when you don’t know the file format or the file does not have a file extension.
Below is the basic syntax of Linux file command:
file [OPTION] [FILE]
- OPTION and additional option which you can pass along with file command.
- FILE is the name of file or files.
How to Use file Command to Find File Type
The file command differentiate files based on first set of tests to return a valid response causes the file type to be printed.
If you will use
file command without any option then it will display the file name along with the file type:
It will return output as below:
/etc/timezone: ASCII text
If you want to show just file type then pass
-b option along with command:
file -b /etc/timezone
It will show output as below and you can see that
/etc/timezone file is a text file.
How to Find File Type of Multiple Files
To get type of multiple files you can pass more than one files along with command as following:
file /etc/timezone /etc/ucf.conf
It will print output in separate line for each file as following:
/etc/timezone: ASCII text /etc/ucf.conf: ASCII text
How to View the Mime Type of a File
You can determine the mime type of a file by passing
--mime)option along with command:
file -i /var/www/index.html
/var/www/index.html: text/html; charset=us-ascii
By this tutorial you have successfully learned how to use the Linux file command. If you want to learn more about file command visit file man page.
Leave a Reply