Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux basic command-find

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

File

Determines the file type of the specified file, which is based on the content of the file, not on the extension. The scope of this command: RedHat, RHEL, Ubuntu, CentOS, SUSE, openSUSE, Fedora.

1. Grammar

File [options] filename

2. List of options

Option

Description

-v |-- version

Display command version information

-- help

Show help documentation

-b |-- brief

When listing the results, the file name is not displayed

-C |-- compile

Write an magic.mgc output file that contains a pre-parsed version of the magic file or directory

-c |-- checking-printout

Printout to check the parsed form of the magic file. This is usually used with the-m flag to debug a new magic file before installing it.

-e |-- exclude testname

Excludes tests specified in testname from the list of tests that determine the file type. Valid test names are:

Apptype, EMX application type (on EMX only).

Text, various types of text files (this test will try to guess the text encoding, regardless of the setting of the Encoding option)

Encoding, different text encodings for soft magic tests

Tokens, looking for known tokens in a text file

Cdf, printing the details of the composite document file

Compress, check and view the compressed file

Elf, print the details of the ELF file.

Soft, check the magic files.

Tar, check the tar file

-F |-- separator

Specifies the delimiter between the file name and the result, default:

-f namefile |-- files-from namefile

From the given file, read the file name, and then operate the

-h |-- no-dereference

Option causes symbolic links not to be followed (on systems that support symbolic links). If the environment variable POSIXLY_TRIDER is not defined, this is the default

-I |-- mime

Causes the file command to output a string of type MIME instead of the more traditional human-readable string. Therefore, it can say 'text/plain;;charset=us-ascii'' instead of 'ASCII text'. To make this option work, the file changes the way it handles files identified by the command itself (for example, many text file types, directories, and so on), and uses another "magic" file. (see the file section below)

-- mime-type |-- mime-encoding

Similar to-I, but only prints the specified elements.

K |-- keep-going

Don't stop the first Standard PvP match, go on. The subsequent match will have the string '012muri'. (if a newline character is required, see the'- r 'option.)

-L |-- dereference

Option causes symbolic links to be followed, like the option of the same name in ls (1) (on systems that support symbolic links). If the environment variable POSIXLY_TRIDER is defined, this is the default

-m |-- magic-file magicfiles

Specifies an alternate list of files and directories that contain magic. This can be a single item or a colon-separated list. If a compiled magic file is found next to a file or directory, it will be used.

-N |-- no-pad

Align the file name in the output

-n |-- no-buffer

After checking each file, force the stdout to be refreshed. This is useful only when checking the list of files. It is used for programs that want to output file types from pipes.

-p |-- preserve-date

On systems that support utime (2) or utime (2), try to keep the access time for parsed files, pretending that the files have never been read.

-r |-- raw

Do not translate non-printable characters into\ ooo. Typically, files convert non-printable characters to their octal representation

-s |-- special-files

Typically, the file only attempts to read and determine the type of parameter file that the STAT (2) report is a normal file. This prevents problems because reading special files can have special consequences. Specifying the-s option causes file to also read parameter files, which are block files or character special files. This is useful for determining the file system type of data in the original disk partitions, which are block special files. This option also causes the file to ignore the file size reported by stat (2) because on some systems it reports that the size of the original disk partition is zero.

-z |-- uncompress

Try to view the compressed file

-0 |-- print0

Outputs the empty character'\ 0' after the end of the file name. Production has been cut very well. This does not affect the delimiters that are still printed.

-z

Try to read the contents of the compressed file

3. Description

File tests each parameter in an attempt to classify it. There are three sets of tests performed in this order: file system tests, magic tests, and language tests. A successful first test will cause the file type to be printed. The type of print usually contains a word text (the file contains only print characters and a few common control characters, and may be safely read on the ASCII terminal), executable (the file contains the result of compiling the program in a form that the UNIX kernel can understand), or data with any other meaning (the data is usually "binary" or non-printable). Exceptions are well-known file formats that contain binary data (core files, tar archives). Be sure to keep these keywords when modifying the magic file or the program itself. The user relies on knowing that all readable files in the directory print the word "text". Don't change "shell command text" to "shell script" like Berkeley did.

The file system test is based on checking the return of the STAT (2) system call. The program checks whether the file is empty or if it is a special file. If any known file types (sockets, symbolic links, or named pipes) that are appropriate for the system you are running are defined in the system header file, they are displayed visually.

Magic tests are used to check files with data in a specific fixed format. A typical example of this is the binary executable (compiled program) a.out file, whose format is defined in # include in the standard include directory, or # include. These files have a "magic number" that is stored in a specific location near the beginning of the file, which tells the UNIX operating system that the file is a binary executable and which of several types it is. The concept of "magic" has been extended to data files. Any file with some invariant identifiers at a small fixed offset can usually be described in this way. The information that identifies these files is read from the compiled magic file / usr/share/misc/magic.mgc, or from the / usr/share/misc/magc directory if the compiled file does not exist. In addition, if $HOME/.magic.mgc or $home/.magic exists, it will take precedence for system magic files. If / etc/magic exists, it will be used with other magic files.

If the file does not match any entries in the magic file, check to see if it is a text file. ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets (such as those used on Macintosh and IBM PC systems), UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC character sets can be distinguished by making up different ranges and byte sequences of each set of printable text. If a file passes any of these tests, its character set is reported. ASCII, ISO-8859-x, UTF-8, and Extended-ASCII files are recognized as "Text" because they are readable on almost any terminal; UTF-16 and EBCDIC are just "character data" because they contain text, but the text needs to be translated to be read. In addition, the file will attempt to determine other characteristics of the text type file. If the line of the file is terminated by CR, CRLF, or Nel instead of the Unix standard LF, this will be reported. Files that contain embedded escape sequences or are overly eye-catching are also identified.

Once the file has determined the character set used in the text type file, it will try to determine in which language the file was written. The language test looks for a specific string (Cf. # include), which can appear anywhere in the first few blocks of the file. For example, the keyword .br indicates that the file is most likely an troff (1) input file, just as the keyword struct stands for a C program. These tests are not as reliable as the first two sets, so they are executed last. Language test routines also test some mixed classes (such as tar (1) archives).

Any file that cannot be identified as written in any of the above character sets is simply called "data".

4. Environmental variables

The environment variable MAGIC can be used to set the default magic file name. If this variable is set, the file will not attempt to open $HOME/.magic. The file adds' .mgc'to the value of this variable as appropriate. The environment variable POSIXLY_RIDER controls (on systems that support symbolic links) whether files try to follow symbolic links. If set, the file will follow symbolic links, otherwise it will not. This is also controlled by the-l and-h options.

5. Return value

The file returns 0 on success and non-zero on error. If the file named by the file Operand does not exist, cannot be read, or cannot determine the type of file named by the file Operand, this is not considered an error that affects the exit status.

6. Documents

/ usr/share/misc/magic.mgc, compiled magic list by default.

/ usr/share/misc/magic, the directory containing the default magic files.

7. Examples

1) View the file directly

[root@localhost weijie] # file 1.c

1.C: ASCII text / / the default delimiter is a colon

[root@localhost weijie] # file-F, 1.c / / specifies that the delimiter is a comma

1.c, ASCII text

2) read the file name from the given file, and then operate

[root@localhost weijie] # cat 1.c

2.c

3.c

[root@localhost weijie] # file-f 1.c / / read the contents of the file from 1.c and re-file them as file names.

2.c: ASCII text

3.c: ASCII text

3) read the type of special file

$file-s / dev/wd0 {bjr d}

/ dev/wd0b: data

/ dev/wd0d: x86 boot sector

$file-s / dev/hda {, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

/ dev/hda: x86 boot sector

/ dev/hda1: Linux/i386 ext2 filesystem

/ dev/hda2: x86 boot sector

/ dev/hda3: x86 boot sector, extended partition table

/ dev/hda4: Linux/i386 ext2 filesystem

/ dev/hda5: Linux/i386 swap file

/ dev/hda6: Linux/i386 swap file

/ dev/hda7: Linux/i386 swap file

/ dev/hda8: Linux/i386 swap file

/ dev/hda9: empty

/ dev/hda10: empty

4) output in MIME format

$file-I file.c file / dev/ {wd0a,hda} / / output MIME type

File.c: text/x-c

File: application/x-executable

/ dev/hda: application/x-not-regular-file

/ dev/wd0a: application/x-not-regular-file

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report