8 organizebydate - Rename files based on their date-time
15 target_template
="%Y/%m/%d/"
24 overwrite_opt
='--no-clobber'
27 echo -n "Usage: organizebydate [-t <template>] [-c | -m | -a] [-v] [-n] [[-i | -x] <num>] <files, directories>
28 --template, -t Target path name using strftime(3) macros
29 Default: $target_template
30 Extra macros accepted:
31 %@ File's directory path
33 --move, --copy Move or copy files, default is copy
34 --overwrite Enable overwrite, default is no-clobber
35 --ctime, -c, --mtime, -m, --atime, -a
36 Timestamping based on change-, modify- (default) or access-time
37 --email, -E Files are raw Emails, timestamping based on Date header
38 --jpeg, -J Files are JPEG images, timestamping based on EXIF tags
39 --touch Change copied (moved) files' mtime
40 --verbose, -v Verbose mode
42 --min, -i Minimum directory level
43 --max, -x Maximum directory level
53 -t|
--target|
--template)
99 echo "$0: unknown option: $1" >&2
118 find "$@" ${minlevel:+-mindepth $minlevel} ${maxlevel:+-maxdepth $maxlevel} -type f
-print | \
123 basedir
=`dirname "$file"`
124 basename=`basename "$file"`
128 case "$files_type" in
130 datetime
=`grep -m 1 -i -E ^Date: "$file" | cut -d' ' -f2-`
133 if type exif
>/dev
/null
2>&1
135 # Date/Time Original, Create Date, Modify Date, Date
136 for tagid
in 0x9003 0x9004 0x0132 0x0006
138 datetime
=`exif -m -t $tagid "$file"`
139 if [ -n "$datetime" ]
145 for tag
in DateTimeOriginal CreateDate ModifyDate Date
147 datetime
=`exiftool -d "%Y-%m-%d %H:%M:%S" -$tag -s3 "$file"`
148 if [ -n "$datetime" ]
154 date=${datetime:0:10}
156 datetime
=${date//:/-}${time}
159 filetime
=`stat -c %$time_field "$file"`
163 if [ -n "$datetime" ]
165 filetime
=`date +%s -d "$datetime"`
168 if [ -z "$filetime" ]
173 target
=`date +"$target_template" -d "@$filetime" | sed -e "s,%@,${basedir//,/\\,},g; s,%\.,${basename//,/\\,},g;"`
178 [ "$copy" = 1 ] && sgn
='='
179 echo "$file $sgn> $target"
181 if [ "${target: -1}" = / ]
185 target_dir
=`dirname "$target"`
187 [ -d "$target_dir" ] || mkdir
-p "$target_dir"
191 cp $verbose $overwrite_opt "$file" "$target"
193 mv $verbose $overwrite_opt "$file" "$target"
195 if [ "$do_touch" = 1 ]
197 touch -t `date +%Y%m%d%H%M.%S -d "@$filetime"` "$target"