at_yasu's blog

ロード的なことを

ファイル整理

写真をずらずら溜め込んでいたら、いつの間にか100や200になり、NFSの接続がぶつぶつ切れるようなって来たので、ファイルをinodeChangeTime*1か変更日のどちらかでディレクトリを作って整理?をするスクリプトを書いた。

Pod::Textが最新版*2でないと動かないので、そこらへんはお気をつけてください。

#!/usr/bin/perl -w
use strict;
use File::Copy;
use Getopt::Long;
use Pod::Text;

our $DEBUG = 0;
my $modified = 0;
my $path = "";
my $verbose = 0;
my $opt = GetOptions (
	'modified' => \$modified,
	'path:s'   => \$path,
	'debug'    => \$DEBUG,
	'verbose'  => \$verbose,
	'usage'    => sub { &usage(); }, 
	'help'     => sub { &usage(); }, 
);

&usage() 
    unless ($path and -d $path);

my %buffer = ();

opendir(DIR, $path) or die $!;
while(my $d = readdir(DIR)) {
	if (-f "${path}/$d") {
		my @st = stat ("${path}/$d");
		my @mod = (gmtime((stat("${path}/${d}"))[(10 - $modified)]))[3..5];
		my $date = sprintf("${path}/%4d:%02d:%02d",($mod[2] + 1900), $mod[1], $mod[0]);
		push (@{$buffer{$date}}, "${path}/${d}");
	}
}
closedir(DIR);

foreach my $k (%buffer) {
	for (@{$buffer{$k}}) {
		print "$k -> $_\n" if $verbose or $DEBUG;
		unless ($DEBUG) {
			mkdir $k unless -d $k;
			move $_, "${k}/$_" or warn $!;
		}
	}
}

sub usage () {
	my $p = Pod::Text->new();
	$p->filter(*DATA);
	exit(0);
}

__DATA__

=head1 Name

dirSplit -- divided to order the file modified time or inode change time in directory.

=head1  VERSION

$Id: dirSplit.pl 171 2008-03-02 07:05:34Z yasui $

=head1 USAGE

    dirSplit --path=. --verbose --modified

=head1 OPTIONS

=over

=item --path <directory path>

Directory has a divided the files.

=item --modified

divided to modified time. default is inode change time.

=item --verbose

verbose output. default is quiet/no output.

=item --usage/--help

show this.

=back

*1:ハードディスクに置いた日。確かにファイルの厳密な作成日って知る事できないよなぁ・・・

*2:正確にはfilterメソッドが使えるようになったバージョン以上でないと動きません。追っかけをする気がないので厳密なバージョンは不明 :-P