keep a whitespace in between the lines of a folded header
[hband-tools.git] / user-tools / organizebydate
blobac8ca3a947a22ca841d69b96ccba8a9b61e9e36a
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 organizebydate - Rename files based on their date-time
10 =cut
12 EOF
15 target_template="%Y/%m/%d/"
16 time_field=Y
17 verbose=''
18 dryrun=0
19 minlevel=''
20 maxlevel=''
21 files_type=''
22 copy=1
23 do_touch=''
24 overwrite_opt='--no-clobber'
26 usage() {
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
32 %. File's name itself
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
41 --dry-run, -n Dry run
42 --min, -i Minimum directory level
43 --max, -x Maximum directory level
47 while [ -n "$1" ]; do
48 case "$1" in
49 -h|--help)
50 usage
51 exit 0
53 -t|--target|--template)
54 shift
55 target_template="$1"
57 -c|--ctime)
58 time_field=Z
60 -m|--mtime)
61 time_field=Y
63 -a|--atime)
64 time_field=X
66 -E|--rfc822|--email)
67 files_type=email
69 -J|--jpeg)
70 files_type=jpeg
72 --touch)
73 do_touch=1
75 -v|--verbose)
76 verbose=-v
78 -n|--dry-run)
79 dryrun=1
81 -i|--min)
82 shift
83 minlevel=$1
85 -x|--max)
86 shift
87 maxlevel=$1
89 --move)
90 copy=0
92 --overwrite)
93 overwrite_opt=''
95 --copy)
96 copy=1
98 -*)
99 echo "$0: unknown option: $1" >&2
100 exit 1
103 shift
104 break
107 break
109 esac
110 shift
111 done
113 if [ -z "$1" ]; then
114 usage
115 exit 1
118 find "$@" ${minlevel:+-mindepth $minlevel} ${maxlevel:+-maxdepth $maxlevel} -type f -print | \
120 set -e
121 while read file
123 basedir=`dirname "$file"`
124 basename=`basename "$file"`
125 datetime=''
126 filetime=''
128 case "$files_type" in
129 email)
130 datetime=`grep -m 1 -i -E ^Date: "$file" | cut -d' ' -f2-`
132 jpeg)
133 if type exif >/dev/null 2>&1
134 then
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" ]
140 then
141 break
143 done
144 else
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" ]
149 then
150 break
152 done
154 date=${datetime:0:10}
155 time=${datetime:10}
156 datetime=${date//:/-}${time}
159 filetime=`stat -c %$time_field "$file"`
161 esac
163 if [ -n "$datetime" ]
164 then
165 filetime=`date +%s -d "$datetime"`
168 if [ -z "$filetime" ]
169 then
170 continue
173 target=`date +"$target_template" -d "@$filetime" | sed -e "s,%@,${basedir//,/\\,},g; s,%\.,${basename//,/\\,},g;"`
175 if [ "$dryrun" = 1 ]
176 then
177 sgn='-'
178 [ "$copy" = 1 ] && sgn='='
179 echo "$file $sgn> $target"
180 else
181 if [ "${target: -1}" = / ]
182 then
183 target_dir=$target
184 else
185 target_dir=`dirname "$target"`
187 [ -d "$target_dir" ] || mkdir -p "$target_dir"
189 if [ "$copy" = 1 ]
190 then
191 cp $verbose $overwrite_opt "$file" "$target"
192 else
193 mv $verbose $overwrite_opt "$file" "$target"
195 if [ "$do_touch" = 1 ]
196 then
197 touch -t `date +%Y%m%d%H%M.%S -d "@$filetime"` "$target"
200 done