This tutorial explains Linux “file” command, options and its usage with examples.
Usage:
file [-c] [-h] [-m mfile] [-f ffile] names
* 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):
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:
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):
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.
- Practice Programming MCQs
- Apply for Programming Internship
- Check Linux Books
- Check Information Technology Books