-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathafv_move
executable file
·248 lines (207 loc) · 8.3 KB
/
afv_move
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env perl
#=======================================================================
# afv_move
# File ID: d4b6028a-5d36-11df-8bab-90e6ba3022ac
# Reads file names from stdin or files and places them into a directory
# structure based on a date in the file name or the modification time.
#
# Character set: UTF-8
# ©opyleft 2004– Øyvind A. Holm <[email protected]>
# License: GNU General Public License, see end of file for legal stuff.
#=======================================================================
use strict;
use warnings;
use File::Copy;
use File::Path;
use Getopt::Std;
$| = 1;
our ($opt_c, $opt_d, $opt_h, $opt_l, $opt_L, $opt_m, $opt_o, $opt_q) =
( 0, "", 0, 0, 0, 0, 0, 0);
our ($opt_s, $opt_S, $opt_v) =
( 0, 0, 0);
getopts('cd:hlLmoqsSv') || die("Option error. Use -h for help.\n");
my $VERSION = "0.5";
our $progname = $0;
$progname =~ s#^.*/(.*?)$#$1#;
my $DEFAULT_DIR = "%Y/%m/%d";
my $skip_dirs = $opt_s ? 1 : 0;
my $skip_files = ($opt_s | $opt_S) ? 1 : 0;
my $simul_str = $skip_files ? " (simulating)" : "";
$opt_h && usage(0);
if ($opt_c + $opt_l + $opt_L > 1) {
die("$0: Can’t mix the \"-c\", \"-l\" or \"-L\" options, " .
"only one or none allowed.\n");
}
LOOP: while (<>) {
my ($Path, $File) =
("", "" );
chomp();
if ($opt_l && ($_ !~ /^\//)) {
warn("$_: Pathname is not absolute\n");
next LOOP;
}
if (/\//) {
if (/^(.*)\/([^\/]+?)$/) {
($Path, $File) =
($1, $2 );
}
} else {
$Path = ".";
$File = $_;
}
if ($opt_m || $File =~ /^(.*?)\b(\d\d\d\d)-?(\d\d)-?(\d\d)T(\d\d):?(\d\d):?(\d\d)Z\b(.+)/) {
# {{{
my ($Pre, $Year, $Mon, $Day, $Hour, $Min, $Sec, $Rest);
unless ($opt_m) {
($Pre, $Year, $Mon, $Day, $Hour, $Min, $Sec, $Rest) =
( $1, $2, $3, $4, $5, $6, $7, $8);
}
my $From = "$Path/$File";
if (-e $From) {
if (-f $From) {
# {{{
if ($opt_m) {
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
$size, $atime, $mtime, $ctime, $blksize, $blocks) =
stat($From);
my @TA = gmtime($mtime);
( $Year, $Mon, $Day, $Hour, $Min, $Sec) =
($TA[5]+1900, $TA[4]+1, $TA[3], $TA[2], $TA[1], $TA[0]);
$Year = sprintf("%04u", $Year);
$Mon = sprintf("%02u", $Mon);
$Day = sprintf("%02u", $Day);
$Hour = sprintf("%02u", $Hour);
$Min = sprintf("%02u", $Min);
$Sec = sprintf("%02u", $Sec);
}
my $Dir = length($opt_d) ? $opt_d : $DEFAULT_DIR;
$Dir =~ s/%Y/$Year/g;
$Dir =~ s/%m/$Mon/g;
$Dir =~ s/%d/$Day/g;
$Dir =~ s/%H/$Hour/g;
$Dir =~ s/%M/$Min/g;
$Dir =~ s/%S/$Sec/g;
$Dir =~ s/%%/%/g;
my $Dest = "$Dir/$File";
if (!$opt_o && -e $Dest) {
$opt_q || warn("$Dest: File already exists, will not overwrite\n");
} else {
# {{{
$skip_dirs ||
-d $Dir ||
mkpath($Dir, $opt_v ? 1 : 0, 0777) ||
die("mkpath(\"$Dir\", 0, 0777): $!");
if ($opt_c) {
$opt_v &&
print("Copying \"$From\" to " .
"\"$Dest\"$simul_str...");
$skip_files ||
copy($From, $Dest) ||
die("\ncopy(\"$From\", \"$Dest\"): $!");
$opt_v && print("OK\n");
} elsif ($opt_L) {
$opt_v &&
print("Linking \"$From\" to " .
"\"$Dest\"$simul_str...");
$skip_files ||
link($From, $Dest) ||
die("\nlink(\"$From\", \"$Dest\"): $!");
$opt_v && print("OK\n");
} elsif ($opt_l) {
$opt_v &&
print("Symlinking \"$From\" to " .
"\"$Dest\"$simul_str...");
$skip_files ||
symlink($From, $Dest) ||
die("\nsymlink(\"$From\", " .
"\"$Dest\"): $!");
$opt_v && print("OK\n");
} else {
$opt_v &&
print("Moving \"$From\" to " .
"\"$Dest\"$simul_str...");
$skip_files ||
move($From, $Dest) ||
die("\nmove(\"$From\", \"$Dest\"): $!");
$opt_v && print("OK\n");
}
# }}}
}
# }}}
} else {
$opt_q || warn("Ignoring non-regular file $From\n");
}
} else {
warn("$From: File not found\n");
}
# }}}
}
}
sub usage {
# Send the help message to stdout {{{
my $Retval = shift;
print(<<END);
$progname v$VERSION
Syntax: $0 [options] [file_with_filenames [...]]
The program reads file names from stdin or from the files on the command
line and moves or copies the files into a directory structure defined by
the user. It can also create soft or hard links if the file system
allows it. The file name has to contain a date on the format
yyyymmddThhmmssZ
which is the date specified in UTC.
Options:
-c Copy files instead of move
-d X Place files under directory X
Use the following modifiers for subtree layout:
%Y Year with four digits
%m Month (00..12)
%d Day of month (00..31)
%H Hour (00..23)
%M Minutes (00..59)
%S Seconds (00..61)
%% Regular percent sign
If the -d option is not specified, "$DEFAULT_DIR" will be used.
-h Show this help.
-l Create symlinks instead of moving or copying files. The file names
in the input has to contain an absolute path to prevent creating
dead links. File names not starting with "/" will be ignored.
-L Create hard links to the files instead of copying or moving.
-m Use the file modification time instead of date found in the file
name. All files will be affected, not only those with a date in
the file name.
-o Overwrite existing files
-q Be quiet, suppress non-critical messages.
-s Simulate, don't really move or copy files.
-S Semisimulate. Don’t touch the files, only create the directory
structure. Useful for running tests with big amounts of data.
-v Verbose execution
These options are likely to change at the moment.
Note: Files on the command line will not be moved themselves, but shall
contain file names of the relevant files to be moved.
Examples:
ls | afv_move -v
find /var/tmp/afvroot | afv_move -vl -d newdir/%Y-%m-%d/%H
afv_move -vL /tmp/filenames.txt -d %Y/%Y-%m-%d
Made by Øyvind A. Holm <sunny\@sunbase.org>
License: GNU General Public License version 2 or later ♥
END
exit($Retval);
# }}}
} # usage()
__END__
=pod
=head1 LICENCE
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
=cut
# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
# End of file afv_move