hacks/bin/file-by-date
2022-01-07 18:07:25 -08:00

24 lines
625 B
Bash
Executable File

#!/bin/bash
set -x
for FN in *; do
if [[ -f "$FN" ]]; then
MTIME="$(stat -f "%Sm" "$FN")"
if [[ ! -z "$MTIME" ]]; then
# eg "Sep 13 05:02:26 2019"
YYYY="$(date -j -f "%b %d %T %Y" "$MTIME" "+%Y")"
MM="$(date -j -f "%b %d %T %Y" "$MTIME" "+%m")"
DD="$(date -j -f "%b %d %T %Y" "$MTIME" "+%d")"
TD="${YYYY}/${YYYY}-${MM}/${YYYY}-${MM}-${DD}"
if [[ ! -d ./"$TD" ]]; then
mkdir -p ./"$TD"
fi
if [[ ! -e ./"${TD}"/"$FN" ]]; then
mv ./"$FN" ./"$TD"/"$FN"
echo mv ./"$FN" ./"$TD"/"$FN"
fi
fi
fi
done