OS X (Unix) shell command: possible to get last opened date of file?

3

2

when you use the following command

find /Users/someUser/someFolder/* -type f -mtime +90

you'll get all the files that have a modification date that is bigger than 90 days. If you open however the file properties in the Finder on OS X for instance there is also a "Last opened" date. Is there a possibility to get all files with a last opened date bigger some treshold like the example above?? Sadly there's no "-otime" :)

Also on Automator you there's no filter for the last open time but just for modified time and created time...

Thanks a lot

Juri

Posted 2009-09-18T14:29:08.660

Reputation: 490

Thanks for the information, really interesting and useful. But the atime you mentioned initially actually works perfectly for me. Thx :) – Juri – 2009-09-18T21:18:36.527

Ok! (Please consider removing the old comments below; you never know if they confuse someone in the future. Thanks!) – Arjan – 2009-10-11T18:22:16.953

Answers

6

The parameter atime defines the last access time. Seems you want that? But that is apparently not the same as the date you use in Finder.

See ls -lu for the date atime uses.

Mac OS X also uses the "HFS meta data" (or: "Finder info") to store dates. For example: Unix does not store file creation dates. The cdate in Unix is really the change date (including, for example, changes in access permissions, so cdate gets a new value in slightly different occasions than the modification date for mdate). Using this metadata, Mac OS X can still keep the details.

There are several options to show (some of) those dates, like:

stat file.txt
GetFileInfo file.txt
mdls file.txt

Using mdfind one can search for specific meta data. But it uses the Spotlight index, so I guess it might not find everything.

Like to find files that are excluded from Time Machine backups:

sudo mdfind "com_apple_backup_excludeItem = 'com.apple.backupd'"

To search based on the creation date, use kMDItemFSCreationDate. For the last opened date: kMDItemLastUsedDate. But note that files which have been created through certain Terminal commands, may not have that meta data set:

echo "Hello world" > ~/Desktop/hello-world.txt
touch ~/Desktop/will-not-be-found.txt
mdfind -onlyin ~/Desktop 'kMDItemFSCreationDate >= $time.this_week'

After opening "will-not-be-found.txt" in Text Edit, you'll see the file after all.

See also the Spotlight Query Syntax.

Arjan

Posted 2009-09-18T14:29:08.660

Reputation: 29 084

That's it. Sorry, should have looked up the man page before. Thx a lot anyway :) – Juri – 2009-09-18T14:38:55.257

ah ok...but it actually doesn't correspond to the "Last opened" time in finder. But could be useful though – Juri – 2009-09-18T14:49:45.127