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

How to use file Command in Linux system

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains the "use of file commands in the Linux system", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "the use of file commands in the Linux system" bar!

Brief introduction to the command:

This command is used to identify the file type, and can also be used to identify the encoding format of some files. It gets the file type by looking at the header information of the file, rather than determining the file type by extension, as Windows does.

Executive permission: All User

Path where the instruction is located: / 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 commonly used parameters.

Examples of use:

1: view help information for the file command

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 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

-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

-k,-- keep-going don't stop at the first match

-L,-- dereference causes symlinks to be 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.

2: do not output the file name, only show the file format and encoding

Through the following two command alignment, you can clearly understand the role of the 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-Server ~] # file-b Temp.txtUTF-8 Unicode text, with very long lines, with CRLF line terminators

3: output a string of type mime

The code is as follows:

[root@DB-Server ~] # file-I Temp.txt Temp.txt: text/plain; charset=utf-8

4: view the file type of the file name in the file

This parameter is very suitable for shell scripts to find and identify data of a certain file type.

You can use the file command to determine the type of file as follows. The screenshot below shows an example of using the file command to determine different file types.

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, 200 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,192 kbps, 44.1 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 interpret 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, was "Temp.txt", from Unix, last modified: Tue Jun 24 00:34:15 2014) [root@DB-Server ~] #

6: view the file type of the soft link to the corresponding file

As shown below, create a soft link sfile and view it with file and file-L with parameters, respectively

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-Server] # file-L sfile sfile: gzip compressed data, was "Temp.txt", from Unix, last modified: Tue Jun 24 00:34:15 2014 [root@DB-Server ~] #

Thank you for reading, the above is the content of "the use of file commands in the Linux system". After the study of this article, I believe you have a deeper understanding of the use of file commands in the Linux system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 218

*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