Randomize group of modification dates I have several files that have the same modification date in a folder, and I would like to change the date for all files within that folder, and the same with other folders in the directory so that each the  content of any folder's modification date is different to any folder.

If that didn't make sense, here's a screenshot of an example directory. The aim is to change the modification date to a different date on the folder's contents and do the same with the other folder's contents with different date.



I know it is possible to touch multiple files in one go by using touch -t YYYYMMDDhhmm file1 file2 file3, however is there a way to automate this process with a random date?
This is an unusual request. I'm not sure why you would want to do this but it sparked my interest. We'll use jot to pick us a random number between 1 and 16000.

jot -r 1 1 16000


This will be the number of hours we subtract from the current date.

seed=$(jot -r 1 1 16000)
date -v-"$seed"H "+%Y%m%d%H%M.%S"


Note that date above, formats the date to work with touch.

Now we use find to list all the directories within the current working directory and use an "inline" script to touch all the filesystem objects within each directory with a unique modification time in each directory.

find . ! -name '.' -type d -exec bash -c ' for dir
    do
        seed=$(jot -r 1 1 16000)
        echo touch -t "$(date -v-"$seed"H "+%Y%m%d%H%M.%S")" "${dir}/"*
    done
' sh {} \;


We are not done yet because we have indiscriminately changes the modification times of nested directories. We want unique mod times for each directory.

find . ! -name '.' -type d -exec bash -c ' for dir
    do
        seed=$(jot -r 1 1 16000)
        echo touch -t "$(date -v-"$seed"H "+%Y%m%d%H%M.%S")" "${dir}"
    done
' sh {} \;


And to put it all together-

#! /bin/ksh

find . ! -name '.' -type d -exec bash -c ' for dir
    do
        seed=$(jot -r 1 1 16000)
        echo touch -t "$(date -v-"$seed"H "+%Y%m%d%H%M.%S")" "${dir}/"*
    done
' sh {} \;


find . ! -name '.' -type d -exec bash -c ' for dir
    do
        seed=$(jot -r 1 1 16000)
        echo touch -t "$(date -v-"$seed"H "+%Y%m%d%H%M.%S")" "${dir}"
    done
' sh {} \; 


This script could fail if you exceed ARG_MAX and currently will only list the changes that touch would make. You can remove the echo in front of touch to make the changes. Please be aware that this script has only limited testing and is offered AS IS.