Introduction to Commands:
This command is used to identify file types and to identify the encoding format of some files. It gets the file type by looking at the header information of the file, not as if Windows determines the file type by extension.
Execute permissions: All User
Instruction Path:/usr/bin/file
Command syntax:
The code is as follows:
file [-bchiklnnprsvz] [f namefile] [-F separator] [-M magicfiles] file ...
Command parameters:
The following table lists some of the commonly used parameters.
Use examples:
The code is as follows:
[Root@db-server ~]# file--help
Usage:file [OPTION] ... [FILE] ...
Determine file type of FILEs.
-M,--magic-file list use list as a colon-separated list of magic number files
-Z,--uncompress try to look inside compressed files
-B,--brief don't prepend filenames to output lines
-C,--checking-printout print the parsed form of the magic file, use with conjunction with-m to debug a new magic file Befo Re Installing it
-F,--files-from file read the filenames to is examined from file
-F,--separator string use string as separator instead of ': '
-I,--mime output MIME type strings
-K,--keep-going don ' t stop at the ' match
-L,--dereference causes Symlinks to is followed-n,--no-buffer do not buffer output
-N,--no-pad do not pad output
-p,--preserve-date preserve access times on files
-R,--raw don ' t translate unprintable chars to Ooo
-S,--special-files treat special (Block/char devices) files as ordinary ones
--help Display this Help and exit
--version output version information and exit
Of course, you can also use man file to get more detailed help documentation information.
2: Do not output file name, only display file format and encoding
With the following two command pairs, you can clearly understand the role of parameter-B.
The code is as follows:
[root@db-server ~]# file Temp.txt temp.txt:utf-8 Unicode text, with very long lines, with CRLF line terminators[root@db-s erver ~]# file-b temp.txtutf-8 Unicode text, with very long lines, with CRLF line terminators
3: Output MIME type string
The code is as follows:
[Root@db-server ~]# file-i Temp.txt temp.txt:text/plain; Charset=utf-8
4: View file types for file names in files
This parameter is ideal for shell scripts to find and discriminate data of a certain file type.
You can use the file command to determine the type of files as follows. The screenshot below shows an example of using the file command to determine different types of files.
The code is as follows:
Tecmint@tecmint ~/linux-tricks $ dir
BACKUP Master.zip
Crossroads-stable.tar.gz Num.txt
Edward-maya-2011-2012-new-remix.mp3 REGGEA.XSPF
Linux-security-optimization-book.gif Tmp-link
Tecmint@tecmint ~/linux-tricks $ file backup/
backup/: Directory
Tecmint@tecmint ~/linux-tricks $ file Master.zip
MASTER.ZIP:ZIP archive data, at least v1.0 to extract
Tecmint@tecmint ~/linux-tricks $ file crossroads-stable.tar.gz
Crossroads-stable.tar.gz:gzip compressed data, from Unix, last Modified:tue Apr 5 15:15:20 2011
Tecmint@tecmint ~/linux-tricks $ file Linux-security-optimization-book.gif
Linux-security-optimization-book.gif:gif image data, version 89a, x 259
Tecmint@tecmint ~/linux-tricks $ file Edward-maya-2011-2012-new-remix.mp3
Edward-maya-2011-2012-new-remix.mp3:audio file with ID3 version 2.3.0, contains:mpeg ADTS, layer III, v1, 44.1 kbps KHz, Jntstereo
Tecmint@tecmint ~/linux-tricks $ file/dev/sda1
/dev/sda1:block Special
Tecmint@tecmint ~/linux-tricks $ file/dev/tty1
/dev/tty1:character Special
5: Try to read the contents of the compressed file
The code is as follows:
[Root@db-server ~]# file-z Temp.txt.gz temp.txt.gz:utf-8 Unicode text, with very long lines, with CRLF line terminators (Gzip compressed data, is "Temp.txt", from Unix, last modified:tue June 24 00:34:15 2014) [Root@db-server ~]#
6: View the file type of the soft link corresponding file
As shown below, create a soft link sfile, and then use the file and the file-l with the parameter respectively to view
The code is as follows:
[Root@db-server ~]# ln-s Temp.txt.gz sfile[root@db-server ~]# file sfile sfile:symbolic link to ' Temp.txt.gz ' [root@db-se RVer ~]# file-l sfile sfile:gzip compressed data, is "Temp.txt", from Unix, last modified:tue June 00:34:15 2014[roo T@db-server ~]#