Use of Linux file Command

  • 2021-08-28 21:45:32
  • OfStack

1. Brief introduction to commands

file command is used to identify file types, and the file inspection is divided into three processes: file system, magic number inspection and language inspection, and can also be used to identify the coding format of some files. It obtains the file type by looking at the header information of the file, instead of determining the file type by extension like Windows.

2. Command format


file [-bchiklLNnprsvz0] [--apple] [--mime-encoding] [--mime-type] [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...
file -C [-m magicfiles]
file [--help]

3. Description of options


-b,--brief: Simple mode, without displaying file names; 
-C,--compile: Generate magic.mgc Files. Matching options -m Use 
-c, --checking-printout Printout of parsing results of magic files 
-e, --exclude [testname]: Excludes checking for files of the specified type, testname The desirable values are apptype , ascii , encoding , tokens , cdf , compress , elf , soft , tar
-F, --separator [separator] Replaces the default after the output file name with the specified delimiter " : " Separator 
-f, --files-from [namefile]: From file namefile Read the file to be detected in, each line 1 A 
-i, --mime : Output mime Instead of a readable string, such as outputting "text/plain; charset=us-ascii" Instead of "ASCII text"
--mime-type, --mime-encoding : Like -i But only the specified elements are printed; 
-k, --keep-going Do not stop on the first match, continue checking 
-l, --list : Print information about the strength of each magic pattern
-L, --dereference : View the file type of the corresponding file of the corresponding soft link 
-m, --magic-file [magicfiles]: Specify magic file . magic file Refers to those files with special contents, such as C File, it will have #include Words; tar The first few bytes of the file will have special rules. And the test magic file Rules are judged according to these special formats 1 The type of the file. And these rules are stored in $HOME/.magic.mgc
-N, --no-pad : Don't pad filenames so that they align in the output
-n, --no-buffer: Forced refresh of standard output stdout . This option is only valid when checking multiple files. You can also use this option when you get the file type through the pipeline 
-p, --preserve-date: Object that retains the file to be detected access time , even if file Command does not change the access time
-r, --raw: Do not convert non-printable characters to \ooo Adj. 8 In the binary form, under normal circumstances, file Will do conversion 
-s, --special-files: Under normal circumstances, file Command only supports detection of ordinary files, just like stat(2)1 Sample. Use this option to make file Command supports special files, such as raw disk partitions, etc. 
-v, --version: Display version information 
-z, --uncompress: Try to interpret the contents of the compressed file 
-0, --print0: Output null characters after file name '\0'
--help Display Help Information 

4. Common examples

(1) Check the file type.


[root@TENCENT64 ~]# file Changelog 
Changelog: ASCII text

(2) No file name is output, only file format and encoding are displayed.


[root@TENCENT64 ~]# file -b Changelog 
ASCII text

(3) Output a string of type mime.


[root@TENCENT64 ~]# file -i Changelog 
Changelog: text/plain; charset=us-ascii

(4) Check the file type of the file corresponding to the soft link.


[root@TENCENT64 ~]# ll Changelog*
-rw-r--r-- 1 root root 1598 Nov 6 22:39 Changelog
lrwxrwxrwx 1 root root  9 Nov 6 23:07 Changelog.ln -> Changelog

# Look at the type of soft link itself 
[root@TENCENT64 ~]# file Changelog.ln
Changelog.ln: symbolic link to `Changelog'

# View the file type of the file corresponding to the soft link 
[root@TENCENT64 ~]# file -L Changelog.ln
Changelog.ln: ASCII text

The above is the use of Linux file command details, more about Linux file command information please pay attention to other related articles on this site!


Related articles: