a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > Perl > 正文

實現fdupe查找重復文件Perl腳本代碼的方法

2020-02-23 19:44:46
字體:
來源:轉載
供稿:網友

fdupe是Perl腳本,一般情況下fdupe是用來檢索指定目錄并查找重復文件的工具,但是我們要如何實現fdupe呢?相信這是很多人都不知道的,那么下面我們就去看看實現fdupe查找重復文件Perl腳本代碼的方法。

圖:


?

?

?

#!/usr/bin/perl
#
# fdupe tool - finding duplicate files
#
# $Id: fdupe,v 1.7 2011/10/14 20:11:21 root Exp root $
#
# Source code Copyright (c) 1998,2011 Bernhard Schneider.
# May be used only for non-commercial purposes with
# appropriate acknowledgement of copyright.
#
# FILE :??????? fdupe
# DESCRIPTION : script finds duplicate Files.
# AUTHOR:?????? Bernhard Schneider <bernhard@neaptide.org>
# hints, crrections & ideas are welcome
#
# usage: fdupe.pl <path> <path> ...
#??????? find / -xdev | fdupe.pl
#
# how to select and remove duplicates:
#?? redirect output to >file, edit the file and mark lines you
#?? wish to move/delete with a preceding dash (-)
#?? Use following script to delete marked files:
#?? #!/usr/bin/perl -n
#?? chomp; unlink if s/^-//;
#
# history:
# 12.05.99 - goto statment replaced with next
# 14.05.99 - minor changes
# 18.05.99 - removed confusing 'for $y'
#??????????? included hash-search
# 20.05.99 - minor changes
# 02.03.00 - some functions rewritten, optimized for speed
# 10.01.01 - hint-fix by Ozzie |ozric at kyuzz.org|
# 05.03.02 - fixed hangups by reading block/char-Devices
# 08.09.11 - skips checking of hard links
# 14.10.11 - accept file names from stdin
#
#use strict; # uncomment for debugging

?

$|=1;
local (*F1,*F2); my %farray = (); my $statF1;

# ------------------------------
# traverse directories
sub scan ($) {
??? my ($dir) = $_[0];
??? opendir (DIR, $dir) or die "($dir) $!:$@";
??? map {
????????? (-d) ? scan ($_) : push @{$farray{-s $_}},$_
???????????? unless (-l or -S? or -p or -c or -b);
??? } map "$dir/$_", grep !/^/./.?$/, readdir (DIR); closedir (DIR);
}

# ------------------------------
# get chunk of bytes from a file
sub getchunk ($$) {
? my ($fsize,$pfname) = @_;
? my $chunksize = 32;
? my ($nread,$buff);

? return undef unless open(F1,$$pfname);

? $statF1 = [(stat? F1)[3,1]];
? binmode F1;
? $nread = read (F1,$buff,$chunksize);
? ($nread == $chunksize || $nread == $fsize) ? "$buff" : undef;
}?

# ------------------------------
# compare two files
sub mycmp ($) {
? my ($fptr) = $_[0];
? my ($buffa, $buffb);
? my ($nread1,$nread2);
? my $statF2;
? my ($buffsize) = 16*1024;

? return -1 unless (open(F2,"<$$fptr"));

? $statF2 = [(stat? F2)[3,1]];

? return 0
? ?if ($statF2->[0] > 1 && $statF1->[1] == $statF2->[1]);

? binmode F2;
? seek (F1,0,0);

? do {? $nread1 = read (F1,$buffa,$buffsize);
??? ?$nread2 = read (F2,$buffb,$buffsize);

??? ?if (($nread1 != $nread2) || ($buffa cmp $buffb)) {
????? ?? return -1;
??????? }
? } while ($nread1);

? return 0;
}

# ------------------------------

print "collecting files and sizes .../n";

if (-t STDIN) {
?$ARGV[0] = '.' unless $ARGV[0]; # use wd if no arguments given
?map scan $_, @ARGV;
} else {?
?while (<STDIN>)? {
??s癧/r/n]$鞍g;
??push @{$farray{-s $_}},$_
?? unless (-l or -S? or -p or -c or -b);
?}
}

print "now comparing .../n";
for my $fsize (reverse sort {$a <=> $b} keys %farray) {

? my ($i,$fptr,$fref,$pnum,%dupes,%index,$chunk);

? # skip files with unique file size
? next if $#{$farray{$fsize}} == 0;

? $pnum? = 0;
? %dupes = %index = ();

? nx:
? for (my $nx=0;$nx<=$#{$farray{$fsize}};$nx++) # $nx now 1..count of files
? {???????????????????????????????????????????? # with the same size
?$fptr = /$farray{$fsize}[$nx];????????? # ref to the first file
??? $chunk = getchunk $fsize,$fptr;
??? if ($pnum) {
?? for $i (@{$index{$chunk}}) {
???????? $fref = ${$dupes{$i}}[0];
????? unless (mycmp $fref) {
??????????? # found duplicate, collecting
???????? push @{$dupes{$i}},$fptr;
???next nx;
????? }
?? }
??? }

??? # nothing found, collecting
??? push @{$dupes{$pnum}},$fptr;
??? push @{$index{$chunk}}, $pnum++;
? }
? # show found dupes for actual size
? for $i (keys %dupes) {
??? $#{$dupes{$i}} || next;
??? print "/n size: $fsize/n/n";
??? for (@{$dupes{$i}}) {
??????? print $$_,"/n";
??? }
? }
}

close F1;
close F2;

上文中小編為大家分享實現fdupe查找重復文件Perl腳本代碼的方法,這個對于查找重復文件是不是很方便呢?這種實用又簡便的辦法,我們何樂而不為呢?

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 日韩亚洲一区二区 | 2019精品手机国产品在线 | 国产美女精品 | 不卡成人| 久久久中文字幕 | 一级片在线观看 | 国产永久免费观看 | av三级| 欧美精品久久久 | 国产一区不卡 | 国产成人精品一区二区 | 成人不卡在线 | 久久久精品久久久久久 | xx视频在线观看 | 亚洲男人av | 亚洲综合在线一区二区 | 亚洲精品视频一区二区三区 | 国产免费一区二区三区最新不卡 | 久久久久中文字幕 | 亚洲一级免费看 | 欧美日韩亚洲成人 | 精品国产一区二区三区电影小说 | 国际精品久久 | 欧美成视频 | h视频在线免费观看 | 射久久 | 亚洲 中文 欧美 日韩在线观看 | 人人鲁人人莫一区二区三区 | 久久道 | 视频精品一区二区三区 | 亚洲精品一区二区网址 | 午夜精品久久久久久久久 | 黄色毛片免费看 | 久久久久综合网 | 夜本色| 一区二区三区免费在线 | 91亚洲精品视频 | 久久免费精品视频 | 日本一区二区三区免费观看 | 免费午夜剧场 | 亚洲精品在线免费 |