#!/bin/bash # # Written by Aaron Bockover # Made available under the MIT/X11 license # # This script recursively fixes up the mtime of # a directory to the same time as the last modified # file in that directory # Function to copy the atime,mtime # from source ($1) to dest ($2) function copytime () { [[ -e "$1" && -e "$2" ]] || { echo "Could not stat one of '$1' or '$2'" 1>&2 exit 1 } echo "$1 -> $2" prog=$(echo " import sys; import os; from stat import ST_ATIME, ST_MTIME; stat = os.stat (sys.argv[1]); os.utime (sys.argv[2], (stat[ST_ATIME], stat[ST_MTIME])); " | tr -d '\n' | tr -d '\t') python -c "$prog" "$1" "$2" || { echo "Could not update time of '$2'" 1>&2 exit 1 } } # find the directories we'll want to modify for dir in $(find -type d -mindepth 0 | sort -r); do # find the oldest immediate child of this directory oldest=$(find $dir \ \( -type f -o -type d \) \ -mindepth 1 -maxdepth 1 \ -exec stat -c"%Y %n" {} \; \ | sort -nr | head -n1 | cut -f2 -d' ') copytime "$oldest" "$dir" done