1847639 Members
3909 Online
110265 Solutions
New Discussion

File command

 
Satish Y
Trusted Contributor

File command

Hi

How does a file command work?. Will it open file for reading? If it opens why the acccess time doesn't change?

Thanks in Advance
Difference between good and the best is only a little effort
6 REPLIES 6
Steven Sim Kok Leong
Honored Contributor

Re: File command

Hi,

Direct from the man page:
==
file performs a series of tests on each file in an attempt to classify it. If file appears to be an ASCII file, file examines the first 512 bytes and tries to guess its language. If file is an executable a.out file, file prints the version stamp, provided it is greater than 0.
==

In short, the file command provides information about its type and version (binary) where possible.

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Dan Hetzel
Honored Contributor

Re: File command

Hi Satish,

'file' opens the file(s) given as arguments, performs a series of test in an attempt to classify them.
For files having a magic number, file uses /etc/magic.

As the file is opened, the access time is modified. If you type 'ls -lutr' in a directory where you issued the file command, you'll see the last accessed file at the end.
(-u flag to reflect last access time instead of last modification)

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
RikTytgat
Honored Contributor

Re: File command

Hi,

The file command uses a file called 'magic' to map the contents of a sequence of bytes at or near the beginning of the file to the type of the file.

For example, if the first bytes of a file are '#!/usr/bin/perl', the file command decides that it is a perl script.

This works for binary files too, as they have a signature to identify them.

Have a look at the /etc/magic file, and you can see for yourself how it does the trick.


Hope this helps,
Rik.
James R. Ferguson
Acclaimed Contributor

Re: File command

Hi Satish:

Interesting, indeed. I too have read the man pages and would expect the last access date as returned by "ls -ul" to change -- it clearly doesn't. The inode timestamp (as returned by "ls -cl" DOES CHANGE. Perhaps 'file' is restoring the lastaccess timestamp after it reads the file (?).

...JRF...
Steven Sim Kok Leong
Honored Contributor

Re: File command

Hi,

From the man page, it appears that the file is opened for reading regardless of whether it is ascii or binary.

I did a test and you are indeed right that the last access time is not updated even though the file is opened. ls -lu does not reflect the new access time when the file command is used.

My guess is that the "file" command "touch" the file back to its original access time.

Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Dan Hetzel
Honored Contributor

Re: File command

Hi James,

You're right. I should have checked instead of simply believing... !-(

'ls -lct' reflects the inode timestamp change

Regards

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com