-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin-by-date
executable file
·105 lines (88 loc) · 3.2 KB
/
bin-by-date
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
#this bash script will sort a collection of files, with specified extensions, into a directory structure as follows
# date1
# hour1
# hour2
# hour3
# date1
# hour1
# hour2
# hour3
#it will only create directories that will have files in them
#Sean Begley
#https://steamforge.net/wiki
#https://github.com/begleysm/ipcam-ftp-tools
#2019-07-17
#first parameter ($1) = path to source directory to be parsed
#second paramter ($2) = path to destination directory for files to be put in (sorted)
#third parameter ($3) = comma delimited list of extensions to sort
#fourth parameter ($4) = mode: [copy, move, move-delete]
# 1.) if set to "copy" then files will be copied from the source dir
# to the destination dir and the source dir will be left unmodified
# 2.) if set to "move" then files will be moved from the source dir
# to the destintion dir but any other files not moved will be left
# unmodified
# 3.) if set to "move-delete" then files will be moved from the source dir
# to the destination dir and then all other files in the source dir will
# be deleted
#example call: ./bin-by-date /path/to/input/ /path/to/output/ jpg,mp4 move-delete
#ensure that $4 has a valid value
if ! [[ $4 = "copy" || $4 = "move" || $4 = "move-delete" ]]; then
echo "Parameter 4 \"$4\" invalid. Please enter \"copy\", \"move\", or \"move-delete\""
exit 1
fi
#create list of extensions from $3
IFS_orig=$IFS
IFS=',' extensions=( $3 )
#command="shopt -s nullglob dotglob; find $1 "
command="find $1 "
for extension in "${extensions[@]}"
do
command+="-name \"*.$extension\" -or "
done
command=${command::-5}
IFS=${IFS_orig}
#grab a list of all files with extensions from $3
files=$(eval $command)
#ensure directory contains our desired extensions files before proceeding
if (( ${#files} ))
then
#get a list of dates (YYYY-MM-DD), file paths, and filenames represented by the image/video files
for each in ${files}
do
date=$(date +%Y-%m-%d-%H -r "$each");
_DATES+=($date);
FILES+=($each);
FILENAMES+=(${each##*/});
done
#create a folder structure
DATES=$(printf "%s\n" "${_DATES[@]}" | sort -u);
for date in ${DATES[@]}; do
#create YYYY-MM-DD dir if it doesnt exist
if [ ! -d "$2/${date:0:10}" ]; then
mkdir "$2/${date:0:10}"
fi
#create YYYY-MM-DD/HH dir if it doesnt exist
if [ ! -d "$2/${date:0:10}/${date:11:2}" ]; then
mkdir "$2/${date:0:10}/${date:11:2}"
fi
done
#move the files into the new folder structure
for ((i=0; i<${#FILES[*]}; i++));
do
date_dir=$(date +%Y-%m-%d -r "${FILES[i]}")
hour_dir=$(date +%H -r "${FILES[i]}")
#check if we should copy or move files
if [[ $4 = "move" || $4 = "move-delete" ]]; then
mv ${FILES[$i]} $2/$date_dir/$hour_dir/"${FILENAMES[$i]}"
elif [ $4 = "copy" ]; then
cp ${FILES[$i]} $2/$date_dir/$hour_dir/"${FILENAMES[$i]}"
fi
done
fi
#check to see if we should delete remaining files in the source directory
if [[ $4 = "move-delete" ]]; then
rm -r $1/*
fi
exit 0