Linux command -- file

Keywords: shell ascii encoding Windows

File is used to view file types. Different from Windows system, Linux system does not reflect the file type through the file name, so you need to use the file command to confirm.

[admin@local ~]$ file exe
exe: Bourne-Again shell script text executable

As above, use the file command to view the type of exe file, which is displayed as an executable bash script file.

Common options
-i: Display file information as MIME type

[admin@local ~]$ file -i exe
exe: text/x-shellscript; charset=us-ascii

-50: Display the type of file corresponding to the soft link

[admin@local ~]$ file ln
ln: symbolic link to `exe'
[admin@local ~]$ file -L ln
ln: Bourne-Again shell script text executable

-f: Execute the file command with each line of the file as a parameter in turn

[admin@local ~]$ vi text
dir
exe
ln
pipe
/etc
etc
[admin@local ~]$ file -f text
dir:  directory
exe:  Bourne-Again shell script text executable
ln:   symbolic link to `exe'
pipe: fifo (named pipe)
/etc: directory
etc:  cannot open `etc' (No such file or directory)

As mentioned above, text is taken as the parameter of the file -f command, and the output is the result of executing file on each line.
Note: if the content in the file is not an absolute path, search in the directory where the file is located

-z: Try to view the contents of the compressed file

[admin@local ~]$ zip text.zip text
  adding: text (deflated 4%)
[admin@local ~]$ file -z text.zip 
text.zip: ASCII text (Zip archive data, at least v2.0 to extract)

Note 1: if there are multiple files in the compressed file, only the information of the first file will be displayed

[admin@local ~]$ zip cmp.zip exe text
  adding: exe (deflated 29%)
  adding: text (deflated 4%)
[admin@local ~]$ file -z cmp.zip 
cmp.zip: Bourne-Again shell script text executable (Zip archive data, at least v2.0 to extract)

Note 2: this command cannot view the contents of the compressed files packed by the tar command

[admin@local ~]$ tar zcf text.tar.gz text
[admin@local ~]$ file -z text.tar.gz 
text.tar.gz: POSIX tar archive (GNU) (gzip compressed data, from Unix, last modified: Wed Jan 24 15:44:41 2018)

Here are the tips after using the help option:

Usage: file [OPTION...] [FILE...]
Determine type of FILEs.

      --help                 display this help and exit
  -v, --version              output version information and exit
  -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                do not prepend filenames to output lines
  -c, --checking-printout    print the parsed form of the magic file, use in
                               conjunction with -m to debug a new magic file
                               before installing it
  -e, --exclude TEST         exclude TEST from the list of test to be
                               performed for file. Valid tests are:
                               ascii, apptype, compress, elf, soft, tar, tokens, troff
  -f, --files-from FILE      read the filenames to be examined from FILE
  -F, --separator STRING     use string as separator instead of `:'
  -i, --mime                 output MIME type strings (--mime-type and
                               --mime-encoding)
      --apple                output the Apple CREATOR/TYPE
      --mime-type            output the MIME type
      --mime-encoding        output the MIME encoding
  -k, --keep-going           don't stop at the first match
  -L, --dereference          follow symlinks (default)
  -h, --no-dereference       don't follow symlinks
  -n, --no-buffer            do not buffer output
  -N, --no-pad               do not pad output
  -0, --print0               terminate filenames with ASCII NUL
  -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
  -C, --compile              compile file specified by -m
  -d, --debug                print debugging messages

Posted by kanikilu on Sun, 05 Apr 2020 15:50:44 -0700