1

I know ls -l will give the modified time of file, but it is giving the time when the file was opened, not closed (using Ctrl+D or something)

Please tell me how to find the time of file closed

[BASH]$date
Wed May  7 10:33:01 EEST 2014
[BASH]$cat >aa
adf
adsf
#waited 8 mins before closing file (time of closing: 10:41)
[BASH]$ls -l aa
-rw-------    1 orclschd staff             9 May  7 10:33 aa
[BASH]$date
Wed May  7 10:41:47 EEST 2014
[BASH]$stat aa
bash: stat: command not found
[BASH]$uname
AIX 

Here I have created a file named aa,

The file was created at 10:33 but when I closed the file and I checked the file's modified date it still showed 10:33.

But the file creation end date (closed time) is 10:41 .

Is there is any command in AIX to find when the file is closed or can we write any bash script to track the file automatically?

  • I don't think the commands you ran support your statement or your question, and what you're asking for isn't clear. Can you have a go at rewording your query with more structure. – EightBitTony May 7 '14 at 8:06
  • simple command or method to find file writing finished date not writing started date – Jeyanthinath May 7 '14 at 8:11
  • Why do you think the cat command finished at 10:41 - there's no indication that was the case. You could have just waited 8 minutes after the cat command before running another date command. – EightBitTony May 7 '14 at 8:13
  • 1
    @HaukeLaging That is only the case because the writing is done on the Enter at the end of the line. I.e. on buffered output this could be later (but probably not with cat) – Anthon May 7 '14 at 8:30
  • 1
    Probably not what you want, but in some very specific circumstances, you can monitor the application that's closing the file with strace -t and trap the close system calls on each file relevant descriptor. However, it slows down the application and implies you know in advance that you want to know the closing time of a particular file. – orion May 7 '14 at 13:14
4

The only information that you can get from a normal file system for a file are the modification (ls -l), access (ls -lu) and status modification (ls -lc) times.

The file doesn't change when closed, so there is nothing in the file's status that reflects that. The current directory stat information doesn't reflect this closing either and reflects the modify and change times from the file (until something else gets changed in the directory).

The modification time will probably be different if you do unbuffered writing to a file, as then the actual disk access might, but doesn't have to be at the moment the file closes. I see no easy way to check that from the commandline though.

BTW. It is easier to use stat for these experiments, as it has seconds granularity, which means you don't have to wait that long to get a visible difference ;-)

  • Thanks for the explanation. As you said istat - AIX , only said the file modified time 10.33 , Is there any possibility to trace it through shell script – Jeyanthinath May 7 '14 at 8:30
  • I would not know what to trace. AFAIK there are no tools like inotify for AIX – Anthon May 7 '14 at 8:34
2

ls -l lists the file's modification time. This is neither the time when the file was opened nor the time the file was closed, but the time the file was last written to. This is usually pretty close to the time the file was closed, but not necessarily identical.

You're probably asking the wrong question. If you want to react when a program has finished writing to a file (i.e. do something when the program has closed the file), the times are irrelevant, what you need is a notification that the file has been closed.

Linux has inotify for this and BSD has kqueue, but AIX has nothing similar as far as I know. There may be an indirect way via the audit system, but this isn't an approach that's likely to work for most use cases.

The simplest way to get program B to react when program A has finished writing is to make program A send a notification to program B. This can be anything you like: a signal, a byte on a pipe or socket, or simply having A launch B or having a script that runs A and then runs B.

If this is not possible, at least arrange for A to write the file to a temporary name, then move it to its final name when it's finished writing. Then have B watch for non-temporary files only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.