The table below shows the metadata attribute which corresponds to the Finder application list view column. The metadata attribute can be passed to the mdls
command to get the appropriate date. However, the date is not with respect to the current location.
Finder List View Column |
Metadata Attribute |
Date Added |
kMDItemDateAdded |
Date Created |
kMDItemContentCreationDate |
Date Last Opened |
kMDItemLastUsedDate |
Date Modified |
kMDItemContentModificationDate |
The function given below outputs the Finder application dates for file names given as input. The dates are converted to the computers default location. This function was tested in both bash
and zsh
.
dates () {
local d=() e=0 f="%+" i n x
d+=("Date Added=kMDItemDateAdded")
d+=("Date Creation=kMDItemContentCreationDate")
d+=("Date Last Opened=kMDItemLastUsedDate")
d+=("Date Modified=kMDItemContentModificationDate")
for n; do
if [[ -e "$n" ]]; then
printf "Name: %s\n" "$n"
for i in "${d[@]}"; do
x="$(mdls -name "${i#*=}" "$n")"
if [[ $x == *\(null\) ]]; then
x="--"
else
x="$(date -jf "%Y-%m-%d %H:%M:%S %z" "${x#* = }" +"$f")"
fi
printf "%-16s = %s\n" "${i%=*}" "$x"
done
else
printf "dates: %s: No such file or directory\n" "$n" >&2
e=1
fi
done
return $e
}
Below is an example of use.
dates x.jpg bad.name mbr.bin
Example output is given below. Note that the file bad.name
does not exist.
Name: x.jpg
Date Added = Mon Dec 11 11:16:37 CST 2023
Date Creation = Mon Oct 2 08:48:39 CDT 2023
Date Last Opened = Mon Dec 11 04:10:54 CST 2023
Date Modified = Mon Dec 11 04:21:26 CST 2023
dates: bad.name: No such file or directory
Name: mbr.bin
Date Added = Fri Apr 2 13:16:56 CDT 2021
Date Creation = Fri Apr 2 13:16:56 CDT 2021
Date Last Opened = --
Date Modified = Fri Apr 2 13:16:56 CDT 2021
To get the format for the dates to be exactly as shown in the Finder on my Mac, I needed to modify the output format. In other words, I needed to replace f="%+"
in the dates
function with the following.
f="%b %-e, %Y at %-l:%M %p"
The change in output is shown below.
Name: x.jpg
Date Added = Dec 11, 2023 at 11:16 AM
Date Creation = Oct 2, 2023 at 8:48 AM
Date Last Opened = Dec 11, 2023 at 4:10 AM
Date Modified = Dec 11, 2023 at 4:21 AM
dates: bad.name: No such file or directory
Name: mbr.bin
Date Added = Apr 2, 2021 at 1:16 PM
Date Creation = Apr 2, 2021 at 1:16 PM
Date Last Opened = --
Date Modified = Apr 2, 2021 at 1:16 PM
Note: While using Catalina 10.15.7 to do testing on APFS volumes, I found the mdls
command can return the wrong "Date Added" for files which have more than one hard link. In other words, the command may return the "Date Added" for a different file hard linked to the same inode. This can happen even when the Finder application shows the correct "Date Added".
References
ls -u
orstat "%Sa"
.