10

What is the easiest way to change the creation and last modification dates of a folder (and recursively contained items) in Mac OS?

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84

6 Answers6

18

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

Community
  • 1
  • 1
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • 1
    To 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
10

I believe following find/touch should work:

find /target/dir -exec touch '{}' \;

From man touch:

touch -- change file access and modification times
anubhava
  • 761,203
  • 64
  • 569
  • 643
10

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 {} \;
Community
  • 1
  • 1
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
5

For those who don't have XCode or developer tools installed and so cannot use SetFile, see below

  1. touch -m abc.txt sets current time as modification time of abc.txt
  2. touch -mt YYYYMMDDhhmm abc.txtsets YYYYMMDDhhmm as modification time of abc.txt
  3. touch -t YYYYMMDDhhmm abc.txt sets YYYYMMDDhhmm as both access and modification time of abc.txt
  4. More importantly, touch command is not designed to change file creation time. It appears, creation time is not a UNIX concept. touch is for changing file access and modification times. See man page for touch.

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.

  1. touch -t to an older date than creation time, sets both creation and modification time to older date.
  2. touch -t to 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 by cp oldfile newfile, so that newfile has current time as creation time which can be then be reduced again with touch -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

kiranpradeep
  • 10,859
  • 4
  • 50
  • 82
2

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

Miguel Prz
  • 13,718
  • 29
  • 42
0

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
}