Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Created June 21, 2018 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtetsuji/3f1f5565bdc074019b743a5d5ddab97e to your computer and use it in GitHub Desktop.
Save xtetsuji/3f1f5565bdc074019b743a5d5ddab97e to your computer and use it in GitHub Desktop.
File duplicate check between directories iCloud and Dropbox. File duplicate state explains by color.
#!/usr/bin/perl
# 2018/06/21 xtetsuji
# Dropbox と iCloud で片方にしか無い、または両方にあるファイルを色分け
# このスクリプトでは WEB+DB PRESS と Software Design で調査する
#
# iCloud で、クラウド側にはファイルはあるけれど手元にデータを
# 同期していない場合、foo.txt の代わりに .foo.txt.icloud というファイルが
# 置かれることに注意
use strict;
use warnings;
use File::Glob qw(bsd_glob);
use File::Basename qw(basename);
use Term::ANSIColor qw(colored);
my $only_icloud_color = "on_green";
my $only_dropbox_color = "on_blue";
# my $icloud_dir = "$ENV{HOME}/Library/Mobile Documents/com~apple~CloudDocs";
# my $dropbox_dir = "$ENV{HOME}/Dropbox";
### Software Design
my $icloud_sd_dir = "$ENV{HOME}/Library/Mobile Documents/com~apple~CloudDocs/書庫/Software Design";
my $dropbox_sd_dir = "$ENV{HOME}/Dropbox/Documents/電子書籍/Software Design";
### WEB+DB PRESS
my $icloud_wd_dir = "$ENV{HOME}/Library/Mobile Documents/com~apple~CloudDocs/書庫/WEB+DB PRESS";
my $dropbox_wd_dir = "$ENV{HOME}/Dropbox/Documents/電子書籍/WEB+DB PRESS";
for ($icloud_sd_dir, $icloud_wd_dir) {
s/(?=[*?~])/\\/g; # bsd_glob に影響を与える文字を escape
#print "=== $_\n";
}
### Software Design
my @icloud_sd_files =
map { s/^\.//; $_ }
map { s/^\.//; s/\.icloud$//; basename($_) }
bsd_glob("$icloud_sd_dir/{.,}Software-Design-*.pdf*");
my @dropbox_sd_files =
map { s/^\.//; $_ }
map { basename($_) }
bsd_glob("$dropbox_sd_dir/Software-Design-*.pdf");
#print ">>> @icloud_sd_files\n";
printf "COLOR: %s, %s, both \n",
colored([$only_icloud_color], "only_icloud"),
colored([$only_dropbox_color], "only_dropbox");
my %sd_files;
for my $file (@icloud_sd_files) {
$sd_files{$file}{icloud}++;
}
for my $file (@dropbox_sd_files) {
$sd_files{$file}{dropbox}++;
}
for my $file (sort keys %sd_files) {
my $in_icloud = $sd_files{$file}{icloud};
my $in_dropbox = $sd_files{$file}{dropbox};
my $in_both = $in_icloud && $in_dropbox;
if ( $in_both ) {
print "$file\n";
} elsif ( $in_icloud ) {
print colored([$only_icloud_color], $file), "\n";
} elsif ( $in_dropbox ) {
print colored([$only_dropbox_color], $file), "\n";
} else {
die;
}
}
### WEB+DB PRESS
my @icloud_wd_files =
map { s/^\.//; $_ }
map { s/\.icloud$//; basename($_) }
bsd_glob("$icloud_wd_dir/{.,}WEB+DB-PRESS-*.pdf*");
my @dropbox_wd_files =
map { basename($_) }
bsd_glob("$dropbox_wd_dir/WEB+DB-PRESS-*.pdf");
#print ">>> @icloud_wd_files\n";
printf "COLOR: %s, %s, both \n",
colored([$only_icloud_color], "only_icloud"),
colored([$only_dropbox_color], "only_dropbox");
my %wd_files;
for my $file (@icloud_wd_files) {
$wd_files{$file}{icloud}++;
}
for my $file (@dropbox_wd_files) {
$wd_files{$file}{dropbox}++;
}
for my $file (sort keys %wd_files) {
my $in_icloud = $wd_files{$file}{icloud};
my $in_dropbox = $wd_files{$file}{dropbox};
my $in_both = $in_icloud && $in_dropbox;
if ( $in_both ) {
print "$file\n";
} elsif ( $in_icloud ) {
print colored([$only_icloud_color], $file), "\n";
} elsif ( $in_dropbox ) {
print colored([$only_dropbox_color], $file), "\n";
} else {
die;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment