In the Linux computer system, the file command prints the file type. It is a useful command when you have to find out the file or identify the file without an extension.
Table of Contents
Syntax of Linux File Command
It is always a tedious task for beginners on the Linux platform to remember the syntax. But when we talk about the syntax of the file command, it is very easy to remember.
file [OPTION] [FILE]
In the syntax above,
- [OPTION] – Specify the option of the file command.
- [FILE] – Specify the file name.
Use the file Command to Find the File Type.
The file command is used when it is hard to judge the file type. It has its own set of tests and algorithms to find out the file type from the provided files. When you use the file command without any option, it will display the file name with its file type. Have a look at the file command below.
It will output as below.
/etc/hosts: ASCII text
To print only the file type, use -b (–brief) option with the file command. See the command below.
file -b /etc/hosts
Output for the command above will be.
According to the output, the ASCII text is a file type returned using the file command.
How to Find the File Type of Multiple Files
To find out the file type of multiple files, you have to provide all file names as multiple arguments.
file /bin/bash /etc/mtools.conf
It will give the following output.
/bin/bash: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=12f73d7a8e226c663034529c8dd20efec22dde54, stripped /etc/mtools.conf: ASCII text
The find command also accepts wildcard characters to find files matching certain words or extensions. For example, finding files with .conf extension in /etc/ directory would have to run the following command.
The command above will print the output as below.
/etc/adduser.conf: ASCII text /etc/apg.conf: ASCII text /etc/appstream.conf: ASCII text /etc/brltty.conf: UTF-8 Unicode text /etc/ca-certificates.conf: UTF-8 Unicode text /etc/debconf.conf: ASCII text /etc/deluser.conf: ASCII text /etc/fuse.conf: ASCII text /etc/gai.conf: ASCII text /etc/hdparm.conf: ASCII text /etc/host.conf: ASCII text /etc/kernel-img.conf: ASCII text /etc/kerneloops.conf: ASCII text /etc/ld.so.conf: ASCII text /etc/libao.conf: ASCII text /etc/libaudit.conf: ASCII text /etc/logrotate.conf: ASCII text /etc/ltrace.conf: ASCII text, with very long lines /etc/mke2fs.conf: ASCII text /etc/mtools.conf: ASCII text /etc/nsswitch.conf: ASCII text /etc/pam.conf: ASCII text /etc/pnm2ppa.conf: ASCII text /etc/popularity-contest.conf: ASCII text /etc/resolv.conf: symbolic link to ../run/systemd/resolve/stub-resolv.conf /etc/rsyslog.conf: ASCII text /etc/sensors3.conf: ASCII text /etc/sysctl.conf: ASCII text /etc/ucf.conf: ASCII text /etc/updatedb.conf: ASCII text /etc/usb_modeswitch.conf: ASCII text
How to view the MIME type of any file
Yes, it is possible to check the MIME type of a file using the file command. The -i option available in the file command, which prints out information on the mime type of specified file. Use the command as below.
file -i /etc/hosts
This command will print mime type text/plain for the hosts file under /etc/hosts directory.
Conclusion on file Command in Linux
There are endless options and combinations are available in Linux using the file command. File has a variety of options, which can be used with sort, ls, cat, and cp command as well.