89 lines
1.6 KiB
Bash
Executable File
89 lines
1.6 KiB
Bash
Executable File
#!/bin/bash
|
|
set -e
|
|
|
|
|
|
_help() {
|
|
self=$( readlink -f "$0" )
|
|
grep " # " "$self" | grep -v [\)]
|
|
exit
|
|
# Moves files to subfolders YYYY-MM-DD/
|
|
# Give filenames as arguments e.x.: *
|
|
#
|
|
}
|
|
|
|
|
|
function gen_unique() {
|
|
if [[ ! -e "$1" ]]; then
|
|
echo "$1"
|
|
return
|
|
fi
|
|
pathdir=$( dirname "$1" )
|
|
fullname="${1##*/}"
|
|
extension="${fullname##*.}"
|
|
filename="${fullname%.*}"
|
|
aZ=( {a..z} {A..Z} )
|
|
idx=0
|
|
while true; do
|
|
newname="${filename}.${aZ[$idx]}.${extension}"
|
|
idx=$(( idx + 1 ))
|
|
if [[ ! -e "$pathdir/$newname" ]]; then
|
|
echo "$pathdir/$newname"
|
|
return
|
|
fi
|
|
if [[ -z "${aZ[$idx]}" ]]; then
|
|
echo "Cannot find unique filename for: $1" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
}
|
|
|
|
|
|
function move_to_dir() {
|
|
pathdir=$( dirname "$1" )
|
|
fullname="${1##*/}"
|
|
date=$( find "$1" -printf '%TY-%Tm-%Td' )
|
|
#date '+%Y-%m-%d' -d "$( date '+%Y/%m/%d %H:%M:%S' -r "$1" ) + 4" )
|
|
mkdir -p "$pathdir/$date"
|
|
tgt_name=$( gen_unique "$pathdir/$date/$fullname" )
|
|
mv -vi "$1" "$tgt_name"
|
|
}
|
|
|
|
|
|
if [[ -z "$1" ]]; then
|
|
_help
|
|
fi
|
|
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--debug)
|
|
# --debug turn on set -x
|
|
set -x
|
|
;;
|
|
--help)
|
|
# --help This help message
|
|
_help
|
|
;;
|
|
-h) # This help message
|
|
_help
|
|
;;
|
|
esac
|
|
done
|
|
|
|
shopt -s nullglob
|
|
|
|
for file in "$@"; do
|
|
if [[ "$file" = "-"* ]]; then
|
|
continue
|
|
fi
|
|
if [[ -d "$file" ]]; then
|
|
continue
|
|
fi
|
|
if [[ "$file" = "*" ]]; then
|
|
continue
|
|
fi
|
|
move_to_dir "$file"
|
|
done
|
|
|
|
|
|
|