@@ -616,13 +616,52 @@ function timeout() {
616616 fi
617617}
618618
619+ ############################################################
620+ # function: Use `touch -d` to apply all sub-dirs recursively
621+ # Params: $1 a source dir
622+ ############################################################
623+ function touchdbyfile() {
624+ if [[ ! -d "$1 " ]]; then return 1 ; fi
625+ local _dir_=${1%/ }
626+ local _old_=$(date '+%Y-%m-%d %H:%M:%S' -r "$1 " 2 >/dev/null)
627+ local _new_=''
628+ local _sub_=''
629+ local _ymd_=''
630+
631+ for f in "${_dir_} "/* ; do
632+ if [[ -d "$f " ]]; then
633+ touchdbyfile "$f "
634+ _new_=$(date '+%Y-%m-%d %H:%M:%S' -r "$f " 2 >/dev/null)
635+ if [[ "${_new_} " > "${_sub_} " ]]; then
636+ _sub_=${_new_}
637+ fi
638+ else
639+ _new_=$(date '+%Y-%m-%d %H:%M:%S' -r "$f " 2 >/dev/null)
640+ if [[ "${_new_} " > "${_ymd_} " ]]; then
641+ _ymd_=${_new_}
642+ fi
643+ fi
644+ done
645+
646+ _ymd_=${_ymd_:- ${_sub_} }
647+ if [[ "${_ymd_} " == "" ]]; then return 2 ; fi
648+
649+ echo ""
650+ if [[ "${_ymd_} " == "${_old_} " ]]; then
651+ echo Matching ${_ymd_} on ${_dir_}
652+ elif [[ "${_ymd_} " > "${_old_} " ]]; then
653+ echo Reserved ${_old_} on ${_dir_}
654+ else
655+ echo Applying ${_ymd_} to ${_dir_} [${_old_} ]
656+ touch -d "${_ymd_} " "${_dir_} "
657+ fi
658+ }
659+
619660############################################################
620661# function: Use `touch -d` on file/dir
621662# Params: a file/dir, or FMT "%Y-%m-%d %H:%M"
622663############################################################
623664function touchd() {
624- local _awk_="awk '{print \$6 ,\$7 }'"
625- local _arg_='-l --time-style=long-iso'
626665 local _datetime_=`date +"%Y-%m-%d %H:%M"`
627666 local _dt_regex_='^[0 -9 ][0 -9 ][0 -9 ][0 -9 ]-[0 -9 ][0 -9 ]-[0 -9 ][0 -9 ]( [0 -9 ][0 -9 ]:[0 -9 ][0 -9 ])?$'
628667 local _date_iso_=''
@@ -632,14 +671,13 @@ function touchd() {
632671 for p in "$@ "; do
633672 if [[ "$p " =~ ${_dt_regex_} ]]; then
634673 _date_iso_="$p "
635- elif [[ -d "$p " ]]; then
636- if [[ "${_date_iso_} " == "" ]]; then
637- IFS=$'\n'
638- for a in `ls -l --time-style=long-iso " $p "|awk '{print $6 , $7 }'`; do
639- _date_iso_=" $a "
640- done
674+ elif [[ -e "$p " ]]; then
675+ if [[ "${_date_iso_} " == "" ]] && [[ ! "" == " ${_dir_file_} " ]] ; then
676+ _date_iso_=$(date '+%Y-%m-%d %H:%M:%S' -r " $p " 2 >/dev/null)
677+ fi
678+ if [[ " ${_dir_file_} " == "" ]]; then
679+ _dir_file_=" ${p %/ } "
641680 fi
642- _dir_file_="$p "
643681 fi
644682 done
645683
@@ -648,6 +686,8 @@ function touchd() {
648686 echo "Applying '${_date_iso_} ' on ${_dir_file_} "
649687 touch -d "${_date_iso_} " "${_dir_file_} " && echo OK
650688 fi
689+ elif [[ -d "${_dir_file_} " ]]; then
690+ touchdbyfile "${_dir_file_} "
651691 else
652692 echo "no-op"
653693 fi
0 commit comments