Skip to content

Commit fcd2249

Browse files
committed
updated touchd func in bashrc.sh
1 parent df82ea2 commit fcd2249

1 file changed

Lines changed: 99 additions & 37 deletions

File tree

tools/bashrc.sh

Lines changed: 99 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ alias bashrc='source ~/.bash_profile; title ${PWD##*/};'
4747
alias brewery='brew update && brew upgrade && brew cleanup'
4848
alias bu='brew upgrade; brew update --debug --verbose'
4949
alias cdp='cd -P .'
50-
alias clean='find . -name *.DS_Store -delete 2>/dev/null; find . -name Thumbs.db -delete 2>/dev/null'
50+
alias clean='find . -type f \( -name *.DS_Store -o -name Thumbs.db \) -delete 2>/dev/null'
5151
alias cls='clear && printf "\e[3J"'
5252
alias conv='iconv -f windows-1252 -t utf-8'
5353
alias dater='date +"%Y-%m-%d %H:%M:%S" -r'
@@ -622,15 +622,18 @@ function timeout() {
622622
# function: Use `touch -d` to apply all sub-dirs recursively
623623
# Params: $1 a source dir path
624624
# $2 optional depth
625+
# $3 options
625626
############################################################
626627
function touchdbyfile() {
627628
if [[ ! -d "$1" ]]; then return 1; fi
628629
local _dir_=${1%/}
629630
local _lvl_=$((${2:-0} - 1))
631+
local _opt_=${3%/}
630632
local _dig_="yes"
631633
local _old_=$(date '+%Y-%m-%d %H:%M:%S' -r "$1" 2>/dev/null)
632-
local _new_=''
634+
local _cur_=''
633635
local _sub_=''
636+
local _new_=''
634637
local _ymd_=''
635638

636639
if [[ ${_lvl_} == 0 ]] || [[ ${_lvl_} == -255 ]]; then
@@ -641,32 +644,55 @@ function touchdbyfile() {
641644
for f in "${_dir_}"/*; do
642645
if [[ -d "$f" ]]; then
643646
if [[ "${_dig_}" == "yes" ]]; then
644-
touchdbyfile "$f" ${_lvl_}
647+
touchdbyfile "$f" ${_lvl_} ${_opt_}
645648
fi
646-
_new_=$(date '+%Y-%m-%d %H:%M:%S' -r "$f" 2>/dev/null)
647-
if [[ "${_new_}" > "${_sub_}" ]]; then
648-
_sub_=${_new_}
649+
fi
650+
if [[ -d "$f" ]]; then
651+
_ymd_=${_sub_}
652+
else
653+
_ymd_=${_cur_}
654+
fi
655+
_new_=$(date '+%Y-%m-%d %H:%M:%S' -r "$f" 2>/dev/null)
656+
if [[ "${_opt_}" == "--asc-sort" ]]; then
657+
if [[ "${_ymd_}" == "" ]] || \
658+
[[ "${_new_}" < "${_ymd_}" ]] ; then
659+
_ymd_=${_new_}
649660
fi
650661
else
651-
_new_=$(date '+%Y-%m-%d %H:%M:%S' -r "$f" 2>/dev/null)
652662
if [[ "${_new_}" > "${_ymd_}" ]]; then
653663
_ymd_=${_new_}
654664
fi
655665
fi
666+
if [[ -d "$f" ]]; then
667+
_sub_=${_ymd_}
668+
else
669+
_cur_=${_ymd_}
670+
fi
656671
done
657672

658-
_ymd_=${_ymd_:-${_sub_}}
659-
if [[ "${_ymd_}" == "" ]]; then return 2; fi
673+
_ymd_=${_cur_:-${_sub_:-${_new_:-${_old_}}}}
660674

661-
echo ""
675+
# echo "_dir_=${_dir_}"
676+
# echo "_ymd_=${_ymd_}"
677+
# echo "_old_=${_old_} ,_new_=${_new_}"
678+
# echo "_cur_=${_cur_} ,_sub_=${_sub_}"
679+
# echo "_opt_=${_opt_}"
680+
681+
local _act_=""
662682
if [[ "${_ymd_}" == "${_old_}" ]]; then
663-
echo Matching ${_ymd_} on ${_dir_}
664-
elif [[ "${_ymd_}" > "${_old_}" ]]; then
665-
echo Reserved ${_old_} on ${_dir_}
666-
else
683+
_act_="Matching ${_ymd_} on ${_dir_}"
684+
fi
685+
if [[ "${_ymd_}" > "${_old_}" ]] && \
686+
[[ "${_opt_}" == "" ]]; then
687+
_act_="Reserved ${_old_} on ${_dir_}"
688+
fi
689+
if [[ "${_act_}" == "" ]]; then
667690
echo Applying ${_ymd_} to ${_dir_} [${_old_}]
668691
touch -d "${_ymd_}" "${_dir_}"
692+
else
693+
echo ${_act_}
669694
fi
695+
echo ""
670696
}
671697

672698
############################################################
@@ -677,12 +703,14 @@ function touchdpath {
677703
if [[ ! -d "$1" ]]; then return 1; fi
678704
local _spath_="$( cd "$( echo "${1}" )" && pwd )"
679705
local _sbase_="$( cd "${_spath_}/.." && pwd )"
706+
local _upper_="$( cd "${_sbase_}/.." && pwd )"
680707
local _slash_=${_spath_//[!\/]}
681708
local _depth_=${#_slash_}
709+
local _order_=$2
682710

683711
if [[ ${_depth_} -gt 2 ]]; then
684-
touchdbyfile "${_spath_}" 1
685-
touchdpath "${_sbase_}"
712+
touchdbyfile "${_spath_}" 1 ${_order_}
713+
touchdpath "${_sbase_}" ${_order_}
686714
fi
687715
}
688716

@@ -693,45 +721,79 @@ function touchdpath {
693721
function touchd() {
694722
local _awk_="awk '{print \$6,\$7}'"
695723
local _arg_='-l --time-style=long-iso'
696-
local _datetime_='date +"%Y-%m-%d %H:%M"'
697-
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])?$'
698-
local _date_iso_=''
724+
local _asc_sort_=''
725+
local _fmt_date_='%Y-%m-%d %H:%M'
726+
local _fmt_regx_='[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]( [0-9][0-9]:[0-9][0-9])?'
727+
local _iso_date_=''
728+
local _dir_from_=''
699729
local _dir_file_=''
730+
local _dir_path_=''
700731
local _dirdepth_=-1
701732

702733
# echo "---args: $@"
703734
for p in "$@"; do
704-
if [[ -e "$p" ]]; then
705-
if [[ "${_date_iso_}" == "" ]] && [[ ! "" == "${_dir_file_}" ]]; then
706-
_date_iso_=`${_datetime_} -r "$p"`
707-
fi
708-
if [[ "${_dir_file_}" == "" ]]; then
735+
if [[ "$p" =~ ^${_fmt_regx_}$ ]]; then
736+
_iso_date_="$p"
737+
elif [[ -e "$p" ]]; then
738+
if [[ -d "$p" ]]; then
739+
if [[ "${_dir_path_}" == "" ]]; then
740+
_dir_path_="${p%/}"
741+
fi
742+
else # non-directory
743+
# echo "File: $p"
744+
if [[ "${_iso_date_}" == "" ]] && \
745+
[[ "${_dir_from_}" == "" ]]; then
746+
_iso_date_=`date +"${_fmt_date_}" -r "$p"`
747+
_dir_from_="${p%/}"
748+
fi
709749
_dir_file_="${p%/}"
710750
fi
711-
elif [[ "$p" =~ ${_dt_regex_} ]]; then
712-
_date_iso_="$p"
751+
# if [[ $p =~ (${_fmt_regx_}) ]] && \
752+
# [[ "${_iso_date_}" == "" ]]; then
753+
# _iso_date_="${BASH_REMATCH[1]} 00:00"
754+
# fi
713755
elif [[ $p =~ ([/-]*L?)([0-9]{1,3}) ]]; then
714756
_dirdepth_=${BASH_REMATCH[2]}
757+
elif [[ "$p" =~ [/-]{1,2}[vV] ]]; then
758+
_asc_sort_='--asc-sort'
759+
elif [[ "$p" =~ [/-]{1,2}[fF] ]]; then
760+
if [[ "${_asc_sort_}" == "" ]]; then
761+
_asc_sort_='--always'
762+
fi
715763
fi
716764
done
717765

718-
if [[ "${_date_iso_}" =~ ${_dt_regex_} ]]; then
719-
if [[ -e "${_dir_file_}" ]]; then
720-
echo "Applying '${_date_iso_}' on ${_dir_file_}"
721-
touch -d "${_date_iso_}" "${_dir_file_}" && echo OK
766+
echo ""
767+
# echo "Date: ${_iso_date_}"
768+
if [[ "${_iso_date_}" =~ ${_fmt_regx_} ]]; then
769+
if [[ -d "${_dir_path_}" ]]; then
770+
echo "Applying '${_iso_date_}' on ${_dir_path_}/*"
771+
touch -d "${_iso_date_}" "${_dir_path_}"/* && echo OK
772+
elif [[ -e "${_dir_file_}" ]]; then
773+
echo "Applying '${_iso_date_}' to ${_dir_file_}"
774+
touch -d "${_iso_date_}" "${_dir_file_}" && echo OK
775+
fi
776+
elif [[ -d "${_dir_path_}" ]]; then
777+
if [[ ! "${_asc_sort_}" == "" ]]; then
778+
echo "+-----------------+"
779+
echo " Sorting by oldest "
780+
echo "+-----------------+"
722781
fi
723-
elif [[ -d "${_dir_file_}" ]]; then
782+
local _dir_base_="$( cd "${_dir_path_}/.." && pwd )"
724783
if [[ ${_dirdepth_} -ne 0 ]]; then
725-
touchdbyfile "${_dir_file_}" ${_dirdepth_}
784+
touchdbyfile "${_dir_path_}" ${_dirdepth_} ${_asc_sort_}
726785
else
727-
touchdpath "${_dir_file_}"
786+
touchdpath "${_dir_path_}" ${_asc_sort_}
728787
fi
729788
else
789+
echo "\$ touchd $@ [args]"
730790
echo ""
731-
echo "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
732-
echo "┃ Syntax: ┃"
733-
echo "┃ touchd <dir> ['yyyy-mm-dd HH:MM'] [-L][<depth>] ┃"
734-
echo "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"
791+
echo "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
792+
echo "┃ Syntax: ┃"
793+
echo "┃ touchd <file>|'yyyy-mm-dd HH:MM' <dir>|<file> ┃"
794+
echo "┃ or: ┃"
795+
echo "┃ touchd <dir> [-L<depth>] [-v] ┃"
796+
echo "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"
735797
echo ""
736798
fi
737799
}

0 commit comments

Comments
 (0)