'mdls' and 'mdimport' interaction: good practice question
Am I correct to assume that, in order to ensure a query in the terminal such as:
mdls -name kMDItemContentCreationDate /var/log/system.log
always returns the actual creation date of said file (provided it exists), I should always force Spotlight to import this information first via:
mdimport -r /var/log/system.log
?
I am interested strictly in obtaining, from the Terminal, that specific piece of data.
This seems to be the conclusion based on another SOV post, especially since the Spotlight indexing may ignore some folders.
Solution 1:
I would use stat
instead of mdls
on a system running the 64bit kernel.
stat -f "%B" file
will return the creation date or birthtime in seconds since epoch (Unix/Epoch time). A human readable output can be produced with
stat -f "%SB" file
You can also format the output of this command by adding the -t option (see man 3 strftime).
On a system running the 32bit kernel, you need to use GetFileInfo
.
GetFileInfo -d file
The output from GetFileInfo
can be converted to Epoch time by running the output though the date
command.
cdate=$(GetFileInfo -d file)
date -j -f "%m/%d/%Y %H:%M:%S" "$cdate" +%s