3

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.

Example directory

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?

2
  • A paid app, A Better Finder Attributes can modify file dates: A Better Finder Attributes 6 gives you total control over file creation and modification dates, setting them to specific times and dates, adding or removing time or simply removing them altogether. Disclaimer: I'm a satisfied user of ABFA. No financial or other connections to Publicspace.net.
    – IconDaemon
    Commented May 29, 2018 at 14:43
  • @IconDaemon Thanks for recommending ABFA, however the reason I need to automate the process is because I have many folders nested within other folders similar to this directory tree example in the question. That, and having to choose a date manually, I may as well use touch instead of ABFA.
    – MrU
    Commented May 29, 2018 at 15:15

1 Answer 1

5

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.

2
  • Ya bloody Legend!! I only wanted for every file for each folder in a directory but I guess I have the option for touching the folders too! As for the reason: Let's just say I go through these files and over time I can sort of predict what is coming next. Only one bug that was easily fixed, the numbers produced from the date generator did not include the decimal for the seconds that touch required, however it was easy as removing the %S to fix it.
    – MrU
    Commented May 30, 2018 at 11:12
  • @MrU Thanks for pointing out the bug. Somehow the period got lost converting ksh's date function into using the external date command for bash.
    – fd0
    Commented May 30, 2018 at 12:29

You must log in to answer this question.

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