mdfind bug: mdfind does not track same modification date as finder, HTML file input, and ls


mdfind modified date metadata should match finder and file picker modified date metadata, but doesn't.


To recreate the problem:


  1. In Finder, create folder 'test' somewhere (ie in Downloads)
  2. In a shell run:
mdls <path to test> | grep kMDItemContentModificationDate

Note that kMDItemContentModificationDate matches Date Modified for the folder as shown in the finder.

3. In Finder, create a subfolder inside the test folder, named 'test1'

Note that the Date Modified changes in the finder. This is also reflected in the 'lastModifiedDate' field of the File object return from a <input type='file'> form input on a webpage if you select the 'test' folder.

4. In a shell run:

mdls <path to test> | grep kMDItemContentModificationDate

Note that kMDItemContentModificationDate has not changed and so no longer matches the Finder or HTML file input.

Note that:

ls -l <path to test>

shows the same modification date as the Finder, not mdfind!


Is this a true bug or am I missing something?


MacBook Pro 15″, macOS 10.15

Posted on Sep 1, 2020 4:34 PM

Reply

Similar questions

10 replies

Sep 2, 2020 11:30 AM in response to VikingOSX

thanks for the code, you will save me some time!

yeah I realized as a workaround, I could supplement mdfind with unix stuff since I knew that ls was showing the correct modification date. Something like:


folder = find_folder_by_name_and_mod_date
if not folder:
   folders = find_folders_by_name_only
   folder = item_from_folders_with_unix_mod_date


Sep 2, 2020 10:27 AM in response to Barney-15E

The UNIX inode tables are far below any metadata content that would concern Spotlight or the mdls command.


If one saves the following Bash script to a text file, and permits it executable, it will show all four UNIX date/time stamps for a given file or folder:


#!/bin/bash
#
# Syntax: http://www.real-world-systems.com/docs/stat.1.html
#    and: man stat
# See: stat(1), strftime(3)

for file in "$@"
do
    stat -t '%Y-%m-%d %X' -f '%n%Sp%T %z %Su %Sg %N %SY %n Last Access:        %Sa %n Last Modify:        %Sm %n Last inode change:  %Sc %n Birth of inode:     %SB%n' "${file}"
done
printf "\n"
exit 0


chmod +x ./lsd.sh
./lsd.sh ~/Desktop/test


The output appears as (I have removed the personal folder path from it), after I add the child test1 folder:


Last Access:        2020-09-02 13:05:25 
Last Modify:        2020-09-02 13:06:33 
Last inode change:  2020-09-02 13:06:33 
Birth of inode:     2020-09-02 13:03:43


Sep 2, 2020 10:20 AM in response to edjoffe

What I meant was that the metadata was not intended for folder contents, not that it may or may not be used. Being that it doesn't appear to have been intended for that purpose, the metadata assigned probably shouldn't be used.


As VikingOSX noted, there is file system data for that, but I've found it may not always be reliable.

You would hope you could at least rely on file system data as it shouldn't be dependent on something choosing to write metadata.

Sep 2, 2020 9:59 AM in response to Barney-15E

If I understand what you are trying to say, the idea is that a folder does not have content and therefore kMDItemContentModificationDate should not change? If that is the case, then a folder should have no value for kMDItemContentModificationDate, not one that doesn't change. Either way, the fact that this value never changes makes it useless, so clearly it would be better to have it be useful, and one would think the useful version would match every other concept of modification date (Finder, file input, ls).

Sep 2, 2020 1:38 PM in response to VikingOSX

2 things...

  1. in spotlight, it seems like you can only search 'modified' by date, not time, so it is not useful for my use case even if I could query mdfind with it (which is unclear how to do).
  2. even weirder... the Date Modified value shown when you "view all in finder" is different from the Date Modified value shown normally in the Finder! As far as I can tell based on values from 'stat', the Date Modified value being shown is the FreeBSD 'st_birthtime', which seems crazy to me. Apple is all over the place, and clearly needs to get their ducks in a row on this stuff.

Sep 2, 2020 2:09 PM in response to edjoffe

Spotlight does not expose time, only date. The UNIX stat command provides a means to extract a specific time to the second if you wish, and you can use that in a loop to check if a file's creation time is in a given range, regardless of date. I wrote a script for another post that does that:


#!/bin/zsh

: <<'COMMENT'
Automator folder action script to test the creation date
time stamp of a file and see if it is in the range of 9:30pm
until 5am. Delete it if it is, and ignore it otherwise.
COMMENT

# path to the drop folder
DROPFOLDER="${1:a:h}"

setopt NO_CASE_GLOB
for f in "${DROPFOLDER}/*.(jpg|jpeg)(.N);
do
    # extract the time as hhmm of the file creation date
    hhmm=$(stat -t '%H%M' -f '%SB' "${f:a}")

    [[ $hhmm -ge 2130 || $hhmm -le 0500 ]] || continue
    # rm -f "${f:a:t}"
    echo "${f:a}" > ~/Desktop/rpt.txt
done
exit 0



When one uses Image Capture to download a file from a camera to macOS, the Finder will attempt to use the EXIF key DateTimeOriginal to set the creation date/time of the file shown by Finder. It will not be the current System time, as would be the case with any other file creation scenario on macOS.


Spotlight adjusts its times to 0000 UTC. Sometimes, once you substract your UTC offset from the Spotlight time, you realize that the date is one day off too, because the adjusted time is before midnight of the date shown by Spotlight. I trust Finder before I would Spotlight, and also, less math involved.

Sep 2, 2020 12:40 PM in response to VikingOSX

The UNIX inode tables are far below any metadata content that would concern Spotlight or the mdls command.

That's what I was trying to convey. Apple's full, robust use of metadata isn't anywhere close to being complete. Use the other data which has a higher probability of being correct. Some (maybe all) of it is duplicated in metadata, so available through Spotlight, but the made up stuff isn't particularly reliable.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

mdfind bug: mdfind does not track same modification date as finder, HTML file input, and ls

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.