How to Determine File Type in Linux using file command - The Tech Influences

0

How to Determine File Type in Linux using file command


Linux file command helps to determine the type of file and its data, the command does not take into account the file extension and instead runs a series of tests to discover the type of file data


If you do not understand then you can see the example for better understanding


Example:- 1

Provides a brief description of the command output file and data type. For example, using the file command to test a text file:

 
[root@red-hat test]# file file.txt
file.txt: ASCII text
[root@red-hat test]#


Example:-2 - Test multiple files

To test multiple files at once, append the file names to the end of the file command:

 
[root@red-hat test]# file file.txt index.html sample.png
file.txt:   ASCII text
index.html: HTML document, UTF-8 Unicode text, with CRLF line terminators
sample.png: PNG image data, 438 x 300, 8-bit/color RGBA, non-interlaced
[root@red-hat test]#


Example:-3 - Test all files in a Directory
 
[root@red-hat test]# file *
Desktop:    directory
Documents:  directory
Downloads:  directory
file.txt:   ASCII text
index.html: HTML document, UTF-8 Unicode text, with CRLF line terminators
Music:      directory
Pictures:   directory
Public:     directory
sample.png: PNG image data, 438 x 300, 8-bit/color RGBA, non-interlaced
Templates:  directory
Videos:     directory
[root@red-hat test]#



Example:-4

To test the contents of that directory, add the path to the directory containing wildcard characters: for example, to test the contents of the example directory, use:


 
[root@red-hat test]# file test/*
test/file1.txt:  empty
test/file.txt:   ASCII text
test/index.html: HTML document, UTF-8 Unicode text, with CRLF line terminators
test/sample.png: PNG image data, 438 x 300, 8-bit/color RGBA, non-interlaced
[root@red-hat test]#



Example:-5 - Test files in a Range

The file command lets you test a subset of files in a directory using regex-style ranges. Select a range by enclosing the values ​​in parentheses. For example, to test files and directories with names in the range [g-z]

 
 
[root@red-hat test]# file [g-z]*
index.html: HTML document, UTF-8 Unicode text, with CRLF line terminators
sample.png: PNG image data, 438 x 300, 8-bit/color RGBA, non-interlaced
test:       directory
[root@red-hat test]#



Example:-6 - Test files in a Multiple Range

The file command lets you test a subset of files in a directory using regex-style ranges. Select a range by enclosing the values ​​in parentheses. For example, to test files and directories with names in the range [g-z]


[root@red-hat test]# file [g-z]* [A-Z]*
index.html: HTML document, UTF-8 Unicode text, with CRLF line terminators
sample.png: PNG image data, 438 x 300, 8-bit/color RGBA, non-interlaced
test:       directory
Desktop:    directory
Documents:  directory
Downloads:  directory
Music:      directory
Pictures:   directory
Public:     directory
Templates:  directory
Videos:     directory
[root@red-hat test]#


Example:-7 - Test Special Files



[root@red-hat test]# file /dev/sr1
/dev/sr1: block special (11/1)
[root@red-hat test]#

Post a Comment

0Comments
Post a Comment (0)