I am new to UNIX.
I want to clarify one thing--
an eg is given as-- $ file monday reports
and the book states monday : text file
                             reports : directory.. How can we know tht its a text file?and directory?
and same as another  $ file calc.c proj newdata
here calc.c is C prog and proj - executable file and newdata is empty file.

So how to classify them ..On what basis are they classified?..
Who is Participating?
shmuklerConnect With a Mentor Commented:
like ahoffmann wrote the file works as following (sources for BSD and Linux implementation are pretty clear and will tell how specifically it works on your system if you are using open source version).

also, to a new Unix'ist I suggest to always try:

$whatis file
(or it could be #whatis socket)

to see all relevant installed manual pages

searching for file returns lots of results. we know that we need file(1) and stat(2)

first file sees whether file is emply, then it could be anything.

if file is not next, file will look for magic number - a signature of sorts. the list of signatures could be in /usr/share/magic or /etc/magic or elsewhere depending on your environment. it would usually be made up from collection of smaller files

if any of the magic numbers matches the content. (some systems can recognize even bootsectors and files from foreign OSes, others only supported executable formats)

next it looks whether it includes characters other than ASCII and some command characters such as new line etc.

If it's ASCII file, then it's probably text.

If nothing matches then we say that it's binary data, as this is a very wide term. :)

$man file               on your system will tell specifics about your version of file(1)
the file command relies on the /etc/magic file which contains patterns to identify a files content

> How can we know tht its a text file?and directory?
not with the file command, obviously (see its name:-)
use ls -l

> here calc.c is C prog and proj - executable file and newdata is empty file.
what do you mean?
If a file is executable (see ls -l) or not doesn't matter the file command.
man magic
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.