2

As a file is selected on Finder, the "Get Info" (Command-i) under the "File" menu brings up the "Info" window. The "Info" window displays the creation time of the file, and also provides a checkbox to lock or unlock the file.

On command line, I have been using GetFileInfo and SetFile to view/set the creation time and lockedness of files and directories. However, GetFileInfo and SetFile are now deprecated (according to their newest manpages on macOS 12 Monterey with Commandline Developer Tools).

Thus, I am looking for new commands on command line to view/set the creation time and lockedness of files and directories.

---- Update ----

I am still looking for a solution to set the creation time of file without using SetFile.

The answer by JRFerguson provided solutions to

  • view the creation time of file
  • view whether a file is locked or unlocked
  • lock or unlock a file

but did not provide a solution to set the creation time.

The manpages of SetFile and GetFileInfo on macOS 12 Monterey with Commandline Developer Tools say:

 Tools supporting Carbon development,
 including /usr/bin/SetFile,
 were deprecated with Xcode 6.
 
 Tools supporting Carbon development,
 including /usr/bin/GetFileInfo,
 were deprecated with Xcode 6.

Xcode 6 was around some time ago. The most recent is Xcode 13, which is for macOS 12 Monterey. Carbon and Cocoa were APIs to develop macOS applications. Carbon was a bridge between Classic Mac (MacOS 9) and MacOS X. Carbon was removed from macOS X 10.15 Catalina. As of macOS 12 Monterey, Cocoa is the sole primary API to develop macOS applications.

Even though SetFile and GetFileInfo are still available on macOS 12 Monterey with Commandline Developer Tools, they are expected to become unavailable in the future.

The above citations from the manpages sound that the commandline commands SetFile and GetFileInfo accompanied Carbon. As Carbon was removed from macOS X 10.15 Catalina, and as Cocoa is now the sole primary API for developing macOS applications, I thought that there should be new commandline commands that accompany Cocoa, replacing SetFile and GetFileInfo. This is why I said "successors" of SetFile and GetFileInfo. Is there any commandline tool accompanying Cocoa, working like SetFile to set the creation time of files and directories?

1
  • See updated, alternative solution.
    – JRFerguson
    Jan 3 at 17:26

1 Answer 1

2

The setfile and getfileinfo commands (with the Command Line Tools installed) are functional on Monterey 12.5.

The easiest way to see their syntax usage information is to do:

/usr/bin/GetFileinfo
/usr/bin/SetFile

That said, ls -OUTl will expose a file's flags and birth (creation) time on the terminal command line. The birth time/date will be in lieu of the modification/accesstime field and the field between the group and size values will be the file flag(s). For a locked file, you will see uchg.

A convenient way to lock a file is chflags uchg ... To unlock, do chflags nouchg ....

ALTERNATIVE SOLUTION

An alternative to using SetFile change to a file's birth (creation) time, is to use touch -t. This will set both the birthtime and modification time to the argument provided.

With touch, the birthtime cannot be set forward of its current value, but can be decremented into the past. SetFile allows changing the time forward or backwards.

The following script can be used to alter a file's birthtime while retaining its original modification time.

cat chtm.sh
#!/usr/bin/env bash
# Set a file's birth (creation) timestamp while leaving the modification
# (mtime) unaltered.

[ $(uname) = "Darwin" ] || { echo "'$0' must be run on macOS"; exit 1; }
[ $# -ne 2 ] && { printf "Usage: $0 [[CC]YY]MMDDhhmm[.SS] file\n"; exit 1; }

t=$1
f=$2
stat -f "BEFORE: btime: %SB, mtime: %Sm" $f || exit 1

m=$(stat -f "%Sm" -t "%Y%m%d%H%M.%S" $f)  # capture the old mtime
touch  -t $t $f || exit 1                 # update  the btime & mtime
touch -mt $m $f || exit 1                 # restore the old mtime

stat -f "AFTER : btime: %SB, mtime: %Sm" $f
exit

A sample run would be:

touch myfile
./chtm.sh 202212311711.00 myfile
BEFORE: btime: Jan  3 12:19:21 2023, mtime: Jan  3 12:19:21 2023
AFTER : btime: Dec 31 17:11:00 2022, mtime: Jan  3 12:19:21 2023

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .