-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganize.sh
75 lines (65 loc) · 2.02 KB
/
organize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#! /bin/bash
: '
possible variations to organize
./organize photos/ using date of last modification
./organize -m photos/ using date of last modification
./organize -c photos/ using date of creation
'
# dolm = date of last modification is default opt
using_dolm=true;
# check directory
function check_dir()
{
# check if directory provided
if [ "$DIRECTORY" = "" ]
then
echo "no directory provided"
exit 1
# check if directory is a directory
elif ! [ -d "$DIRECTORY" ]
then
echo "$DIRECTORY is not a directory";
exit 1
fi
}
# look for -c and -m opts
while getopts "cm" opt; do
case $opt in
# -c opt organizes using creation date
c) DIRECTORY="$2"; using_dolm=false; check_dir; echo "organizing using date of creation...";;
# -m opt organizes using last modified date
m) DIRECTORY="$2"; check_dir; echo "organizing using date of last modification...";;
esac
done
# if no flags provided, proceeed with default opt
if (( $OPTIND == 1 )); then
DIRECTORY=$1
check_dir
echo "organizing using date of last modification..."
fi
# count number of files organized
counter=1
if [ $using_dolm == true ]
then
# organizing using date of last modification
for photo in "$DIRECTORY"*.[jJ][pP][gG] "$DIRECTORY"*.[jJ][pP][eE][gG] "$DIRECTORY"*.[pP][nN][gG]; do
d=$(date -r "$photo" +%Y-%m)
dirname="${DIRECTORY}${d}"
mkdir -p -- "$dirname"
mv -- "$photo" "$dirname/"
((counter++))
done
echo "$counter files organized using date of last modification"
exit 0
else
# organizing using date of last creation
for photo in "$DIRECTORY"*.[jJ][pP][gG] "$DIRECTORY"*.[jJ][pP][eE][gG] "$DIRECTORY"*.[pP][nN][gG]; do
d=$(stat -f %SB -t %Y-%m "$photo")
dirname="${DIRECTORY}${d}"
mkdir -p -- "$dirname"
mv -- "$photo" "$dirname/"
((counter++))
done
echo "$counter files organized using date of creation"
exit 0
fi