What is the easiest way to change the creation and last modification dates of a folder (and recursively contained items) in Mac OS?
-
do you want to do this programmatically (via code, Objective-C, etc.) or Applescript or some other way? – Michael Dautermann Aug 03 '13 at 08:22
-
4Why 2 downvotes? This is a perfect good question. – P i Nov 19 '15 at 11:08
6 Answers
I am not sure whether this is problem with Mavericks or what, but touch -mt OR touch -t just update the modified and last opened time.
Maybe touch -mt OR touch -t commands are working with 10.8.4 and earlier.
For Mavericks, I found solution as below.
SetFile -d '12/31/1999 23:59:59' file.txt
MM dd yyyy hh mm ss fileName
To update all files in folder just use
SetFile -d '12/31/1999 23:59:59' *
Reference
- 1
- 1
- 30,974
- 45
- 160
- 276
-
1To set the creation date/time to the date/time of a reference file, use `SetFile -d "$(GetFileInfo REFERENCEFILE | grep created | cut -c10-)" TARGETFILE` – Tim Sep 08 '18 at 11:13
I believe following find/touch should work:
find /target/dir -exec touch '{}' \;
From man touch:
touch -- change file access and modification times
- 761,203
- 64
- 569
- 643
-
7This doesn't change the creation date as reported by the Finder, only the modification date (observed on OS X 10.9.1). – Pol Feb 08 '14 at 19:51
-
1
The easiest way would be using the Terminal:
// to change the creation date
touch -mt 201308030000 [pathtofile][filename]
// to change the modified date
touch -t 201308030000 [pathtofile][filename]
The date/time string is build like that:
- year YYYY
- month MM
- day DD
- hour hh
- minute mm
Edit
And for the recursive part, use what anubhava suggested in his answer:
find [path] -exec touch -t 201308030000 {} \;
- 1
- 1
- 23,204
- 9
- 87
- 126
For those who don't have XCode or developer tools installed and so cannot use SetFile, see below
touch -m abc.txtsets current time as modification time of abc.txttouch -mt YYYYMMDDhhmm abc.txtsets YYYYMMDDhhmm as modification time of abc.txttouch -t YYYYMMDDhhmm abc.txtsets YYYYMMDDhhmm as both access and modification time of abc.txt- More importantly,
touchcommand is not designed to change file creation time. It appears, creation time is not a UNIX concept.touchis for changing file access and modification times. See man page fortouch.
Then why touch changes creation time only some times ? Reason is modification time cannot be lesser than creation time. So when we set modification time to a date before creation time, the creation time is also set to modification time. Two things now.
touch -tto an older date than creation time, sets both creation and modification time to older date.touch -tto a newer date than creation time, sets only modification time to newer date. Creation time will remain unaffected. The best creation time can be reached bycp oldfile newfile, so thatnewfilehas current time as creation time which can be then be reduced again withtouch -t
SetFile works well with any date. In case it is not in PATH use as
xcrun SetFile -d '12/31/2099 23:59:59' abc.txt
- 10,859
- 4
- 50
- 82
From the terminal:
a) Creation date:
touch –t YYYYMMDDhhmm theFile
b) Modified date:
touch –mt YYYYMMDDhhmm the File
Examples:
touch –t 201308021025 theFile.txt
touch –mt 201308021026 theFile.txt
If you need to do on a set of files, use find & xargs
- 13,718
- 29
- 42
In latest macOS Big Sur 11.0.1 running on 27" iMac (late 2019 model), directory listings through commands "ls -lt" and "ls -ltr" are exactly the same instead of reverse.
So I decided to use command "stat" to generate integer numbers correspond to modification dates instead of using unreliable "ls" command (correct me if I am wrong). I have created following non-recursive function in .bashrc configuration file in my home directory as alias "moddate" ready to be executed anytime on any directory list as: "moddate *" for all folders in current directory. Note only folder make sense here but not file.
### Update parent folder's modification date taken from the newest child folder nested within.
function moddate {
dir_list=$@
for path in $dir_list
do
if [[ -d "${path}" ]]; then
latest=`stat -f '%m %N' ${path}/* | sort -k1rn | awk '{print $2; exit}'`
ls -ld $latest
(set -x; touch -r "${latest}" "${path}")
fi
done
}
- 1
- 1