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?
Best Answer
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.This will be the number of hours we subtract from the current date.
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.We are not done yet because we have indiscriminately changes the modification times of nested directories. We want unique mod times for each directory.
And to put it all together-
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 oftouch
to make the changes. Please be aware that this script has only limited testing and is offered AS IS.