5 Practical “file” Command Usage Examples in Linux

«
»

This tutorial explains Linux “file” command, options and its usage with examples.

“file” command is used To determine file type. This post describes “file” command used in Linux along with usage examples and/or output.

Usage:
file [-c] [-h] [-m mfile] [-f ffile] names

advertisement

* If the file cannot be read, its status undetermined, or its type undetermined, file will indicate that the file was processed and its type was undetermined.
* File must be able to determine the types directory, FIFO, socket, block special file, and character special file
* The file is identified as a data file
* Zero-length files are identified as such
* In most implementations, the file command uses a database to drive the probing of the lead bytes. That database is implemented in a file called magic, whose location is usually in /etc/magic, /usr/share/file/magic or a similar location.

Here’s the listing of example usage of “file” command:

1. To determine file type(file file_name/directory_name):

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
sanfoundry-> file india_map.cpp 
india_map.cpp: ASCII text
sanfoundry-> file tex.c
tex.c: C program text

2. To show whether file empty or not:

sanfoundry-> touch file.txt 
sanfoundry-> file file.txt 
file.txt: empty

3. To find drives in /dev and there types:

advertisement
sanfoundry-> file /dev/hidraw0 
/dev/hidraw0: character special

4. To specify a file specially formatted containing position-sensitive tests(file -m File_name):

sanfoundry-> cat > 2.c 
Hi i am x.
I am a nice boy.
sanfoundry-> file -m 2.c 
2.c, 1: Warning: offset `Hi i am x.' invalid
2.c, 1: Warning: type `Hi i am x.' invalid
2.c, 2: Warning: offset `I am a nice boy.' invalid
2.c, 2: Warning: type `I am a nice boy.' invalid
file: could not find any magic files!

5. To perform default position-sensitive and context-sensitive tests to the given file(file -d file_name):

advertisement
sanfoundry-> file -d 2.c 
........
 
> 0 search/1,!p,""]
18446744073709551576 != 0 = 1
mget @0: Hi i am x.\nI am a nice boy.\n\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000
 
>> 0 regex,=^package[ \t]+req,"Tcl script"]
1 == 0 = 0
ascmagic 1
2.c: ASCII text

NOTE
Some more useful options:
* -h, do-not-dereference symbolic links that point to an existing file or directory.
* -L, dereference the symbolic link that points to an existing file or directory.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

advertisement
If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.