Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 5 May 2023 03:01:14 -0400
From:      Paul Procacci <pprocacci@gmail.com>
To:        Kaya Saman <kayasaman@optiplex-networks.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Tool to compare directories and delete duplicate files from one directory
Message-ID:  <CAFbbPug8npwyxjzxB20wV%2BmHpqovoCwPyx1p89GRSKDWhA_qcQ@mail.gmail.com>
In-Reply-To: <b4ac4aea-a051-fbfe-f860-cd7836e5a1bb@optiplex-networks.com>
References:  <9887a438-95e7-87cc-a162-4ad7a70d744f@optiplex-networks.com> <CAFbbPugfhXGPfscKpx6B0ue=DcF_qssL6P-0GgB1CWKtm3U=tQ@mail.gmail.com> <344b29c6-3d69-543d-678d-c2433dbf7152@optiplex-networks.com> <CAFbbPuiNqYLLg8wcg8S_3=y46osb06%2BduHqY9f0n=OuRgGVY=w@mail.gmail.com> <ef0328b0-caab-b6a2-5b33-1ab069a07f80@optiplex-networks.com> <CAFbbPujUALOS%2BsUxsp=54vxVAHe_jkvi3d-CksK78c7rxAVoNg@mail.gmail.com> <7747f587-f33e-f39c-ac97-fe4fe19e0b76@optiplex-networks.com> <CAFbbPuhoMOM=wp26yZ42e9xnRP%2BtJ6B30y8%2BBa3eCBh2v66Grw@mail.gmail.com> <fd9aa7d3-f6a7-2274-f970-d4421d187855@optiplex-networks.com> <CAFbbPujpPPrm-axMC9S5OnOiYn2oPuQbkRjnQY4tp=5L7TiVSg@mail.gmail.com> <eda13374-48c1-1749-3a73-530370934eff@optiplex-networks.com> <CAFbbPujbyPHm2GO%2BFnR0G8rnsmpA3AxY2NzYOAAXetApiF8HVg@mail.gmail.com> <b4ac4aea-a051-fbfe-f860-cd7836e5a1bb@optiplex-networks.com>

next in thread | previous in thread | raw e-mail | index | archive | help
--0000000000009af6d205faecdbf6
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Fri, May 5, 2023 at 12:06=E2=80=AFAM Kaya Saman <kayasaman@optiplex-netw=
orks.com>
wrote:

>
> On 5/5/23 04:36, Paul Procacci wrote:
> > #!/usr/bin/env perl
> >
> > use strict;
> > use warnings;
> >
> > sub msgDie
> > {
> >   my ($ret) =3D shift;
> >   my ($msg) =3D shift // "$0 dir_base dir\n";
> >   print $msg;
> >   exit($ret);
> > }
> >
> > msgDie(1) unless(scalar @ARGV eq 2);
> >
> > my $base =3D $ARGV[0];
> > my $dir  =3D $ARGV[1];
> >
> > msgDie(1, "base directory doesn't exist\n") unless -d $base;
> > msgDie(1, "source directory doesn't exist\n") unless -d $dir;
> >
> > opendir(my $dh, $dir) or msgDie("Unable to open directory: $dir\n");
> > while(readdir $dh)
> > {
> >   next if($_ eq '.' || $_ eq '..');
> >   next if(! -f "$base/$_");
> >
> >   my ($ref) =3D (stat("$base/$_"))[7];
> >   my ($src) =3D (stat("$dir/$_"))[7];
> >   unlink("$dir/$_") if($ref =3D=3D $src);
> > }
>
>
> To start with this is the directory structure:
>
>
>   ls -lhR /tmp/test1
> total 1
> drwxr-xr-x  2 root  wheel     3B May  5 04:57 dupdir1
> drwxr-xr-x  2 root  wheel     3B May  5 04:57 dupdir2
>
> /tmp/test1/dupdir1:
> total 1
> -rw-r--r--  1 root  wheel     8B Apr 30 03:17 dup
>
> /tmp/test1/dupdir2:
> total 1
> -rw-r--r--  1 root  wheel     7B May  5 03:23 dup1
>
>
> ls -lhR /tmp/test2
> total 1
> drwxr-xr-x  2 root  wheel     3B May  5 04:56 dupdir1
> drwxr-xr-x  2 root  wheel     3B May  5 04:56 dupdir2
>
> /tmp/test2/dupdir1:
> total 1
> -rw-r--r--  1 root  wheel     4B Apr 30 02:53 dup
>
> /tmp/test2/dupdir2:
> total 1
> -rw-r--r--  1 root  wheel     7B Apr 30 02:47 dup1
>
>
> So what I want to happen is the script to recurse from the top level
> directories test1 and test2 then expected behavior should be to remove
> file dup1 as dup is different between directories.
>
>
> I ran the script and again it didn't produce any output?
>
> ./test.pl /tmp/test1 /tmp/test2
>
>
> I'm not sure if I need any other perl module? It is installed:
>
> p5-ExtUtils-Config-0.008_1     Wrapper for perl configuration
> perl5-5.32.1_3                 Practical Extraction and Report Language
>
>
> I have many p5- modules too installed but maybe I don't have the right
> ones to run the script?
>
>
>

- My script doesn't have any output.
- My script doesn't recurse directories.
- My script requires no modules.
- You just need perl5 installed.

So you want something like this:

% find test* -type f -ls
131318        9 -rwxr-xr-x    1 pprocacci                        pprocacci
                            763 May  5 06:58 test.pl
132503        9 -rw-r--r--    1 pprocacci                        pprocacci
                              5 May  5 06:52 test1/dupdir2/dup1
132898        9 -rw-r--r--    1 pprocacci                        pprocacci
                              8 May  5 06:51 test1/dupdir1/dup
132771        9 -rw-r--r--    1 pprocacci                        pprocacci
                              4 May  5 06:52 test2/dupdir1/dup
133006        9 -rw-r--r--    1 pprocacci                        pprocacci
                              5 May  5 06:52 test2/dupdir2/dup1

And after the run of the perl script like so:
% ./test.pl test1 test2

You want it to be like this:

% find test* -type f -ls
131318        9 -rwxr-xr-x    1 pprocacci                        pprocacci
                            763 May  5 06:58 test.pl
132503        9 -rw-r--r--    1 pprocacci                        pprocacci
                              5 May  5 06:52 test1/dupdir2/dup1
132898        9 -rw-r--r--    1 pprocacci                        pprocacci
                              8 May  5 06:51 test1/dupdir1/dup
132771        9 -rw-r--r--    1 pprocacci                        pprocacci
                              4 May  5 06:52 test2/dupdir1/dup

If so:

####################################################
#!/usr/bin/env perl

use strict;
use warnings;

sub msgDie
{
  my ($ret) =3D shift;
  my ($msg) =3D shift // "$0 dir_base dir\n";
  print $msg;
  exit($ret);
}

sub doit
{
  my($base, $cur) =3D @_;

  opendir(my $dh, $cur) or msgDie("Unable to open directory: $cur\n");
  while(readdir $dh)
  {
    next if($_ eq '.' || $_ eq '..');
    if(-d "$cur/$_"){ doit("$base/$_", "$cur/$_"); next; }
    next if(! -f "$base/$_");

    my ($ref) =3D (stat("$base/$_"))[7];
    my ($src) =3D (stat("$cur/$_"))[7];
    unlink("$cur/$_") if($ref =3D=3D $src);
  }
}

msgDie(1) unless(scalar @ARGV eq 2);

my $base =3D $ARGV[0];
my $dir  =3D $ARGV[1];

msgDie(1, "base diretory doesn't exist\n") unless -d $base;
msgDie(1, "source diretory doesn't exist\n") unless -d $dir;

doit($base, $dir);
####################################################

~Paul

--=20
__________________

:(){ :|:& };:

--0000000000009af6d205faecdbf6
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div dir=3D"ltr"><br><div><br></div></div><br><div cl=
ass=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Fri, May 5, 20=
23 at 12:06=E2=80=AFAM Kaya Saman &lt;<a href=3D"mailto:kayasaman@optiplex-=
networks.com">kayasaman@optiplex-networks.com</a>&gt; wrote:<br></div><bloc=
kquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:=
1px solid rgb(204,204,204);padding-left:1ex"><br>
On 5/5/23 04:36, Paul Procacci wrote:<br>
&gt; #!/usr/bin/env perl<br>
&gt;<br>
&gt; use strict;<br>
&gt; use warnings;<br>
&gt;<br>
&gt; sub msgDie<br>
&gt; {<br>
&gt; =C2=A0 my ($ret) =3D shift;<br>
&gt; =C2=A0 my ($msg) =3D shift // &quot;$0 dir_base dir\n&quot;;<br>
&gt; =C2=A0 print $msg;<br>
&gt; =C2=A0 exit($ret);<br>
&gt; }<br>
&gt;<br>
&gt; msgDie(1) unless(scalar @ARGV eq 2);<br>
&gt;<br>
&gt; my $base =3D $ARGV[0];<br>
&gt; my $dir =C2=A0=3D $ARGV[1];<br>
&gt;<br>
&gt; msgDie(1, &quot;base directory doesn&#39;t exist\n&quot;) unless -d $b=
ase;<br>
&gt; msgDie(1, &quot;source directory doesn&#39;t exist\n&quot;) unless -d =
$dir;<br>
&gt;<br>
&gt; opendir(my $dh, $dir) or msgDie(&quot;Unable to open directory: $dir\n=
&quot;);<br>
&gt; while(readdir $dh)<br>
&gt; {<br>
&gt; =C2=A0 next if($_ eq &#39;.&#39; || $_ eq &#39;..&#39;);<br>
&gt; =C2=A0 next if(! -f &quot;$base/$_&quot;);<br>
&gt;<br>
&gt; =C2=A0 my ($ref) =3D (stat(&quot;$base/$_&quot;))[7];<br>
&gt; =C2=A0 my ($src) =3D (stat(&quot;$dir/$_&quot;))[7];<br>
&gt; =C2=A0 unlink(&quot;$dir/$_&quot;) if($ref =3D=3D $src);<br>
&gt; }<br>
<br>
<br>
To start with this is the directory structure:<br>
<br>
<br>
=C2=A0=C2=A0ls -lhR /tmp/test1<br>
total 1<br>
drwxr-xr-x=C2=A0 2 root=C2=A0 wheel=C2=A0=C2=A0=C2=A0=C2=A0 3B May=C2=A0 5 =
04:57 dupdir1<br>
drwxr-xr-x=C2=A0 2 root=C2=A0 wheel=C2=A0=C2=A0=C2=A0=C2=A0 3B May=C2=A0 5 =
04:57 dupdir2<br>
<br>
/tmp/test1/dupdir1:<br>
total 1<br>
-rw-r--r--=C2=A0 1 root=C2=A0 wheel=C2=A0=C2=A0=C2=A0=C2=A0 8B Apr 30 03:17=
 dup<br>
<br>
/tmp/test1/dupdir2:<br>
total 1<br>
-rw-r--r--=C2=A0 1 root=C2=A0 wheel=C2=A0=C2=A0=C2=A0=C2=A0 7B May=C2=A0 5 =
03:23 dup1<br>
<br>
<br>
ls -lhR /tmp/test2<br>
total 1<br>
drwxr-xr-x=C2=A0 2 root=C2=A0 wheel=C2=A0=C2=A0=C2=A0=C2=A0 3B May=C2=A0 5 =
04:56 dupdir1<br>
drwxr-xr-x=C2=A0 2 root=C2=A0 wheel=C2=A0=C2=A0=C2=A0=C2=A0 3B May=C2=A0 5 =
04:56 dupdir2<br>
<br>
/tmp/test2/dupdir1:<br>
total 1<br>
-rw-r--r--=C2=A0 1 root=C2=A0 wheel=C2=A0=C2=A0=C2=A0=C2=A0 4B Apr 30 02:53=
 dup<br>
<br>
/tmp/test2/dupdir2:<br>
total 1<br>
-rw-r--r--=C2=A0 1 root=C2=A0 wheel=C2=A0=C2=A0=C2=A0=C2=A0 7B Apr 30 02:47=
 dup1<br>
<br>
<br>
So what I want to happen is the script to recurse from the top level <br>
directories test1 and test2 then expected behavior should be to remove <br>
file dup1 as dup is different between directories.<br>
<br>
<br>
I ran the script and again it didn&#39;t produce any output?<br>
<br>
./<a href=3D"http://test.pl" rel=3D"noreferrer" target=3D"_blank">test.pl</=
a> /tmp/test1 /tmp/test2<br>
<br>
<br>
I&#39;m not sure if I need any other perl module? It is installed:<br>
<br>
p5-ExtUtils-Config-0.008_1=C2=A0=C2=A0=C2=A0=C2=A0 Wrapper for perl configu=
ration<br>
perl5-5.32.1_3=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Practical Extraction and Report Langua=
ge<br>
<br>
<br>
I have many p5- modules too installed but maybe I don&#39;t have the right =
<br>
ones to run the script?<br>
<br>
<br>
</blockquote></div><br><br clear=3D"all">
<div>- My script doesn&#39;t have any output.<br>


- My script doesn&#39;t recurse directories.

<br>
- My script requires no modules.

</div><div>- You just need perl5 installed.<br></div><br></div>So you want =
something like this:<br><div><br>% find test* -type f -ls<br>131318 =C2=A0 =
=C2=A0 =C2=A0 =C2=A09 -rwxr-xr-x =C2=A0 =C2=A01 pprocacci =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0pprocacci=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 763 May =C2=A05 06:58 <a href=3D"http://test.pl">t=
est.pl</a><br>132503 =C2=A0 =C2=A0 =C2=A0 =C2=A09 -rw-r--r-- =C2=A0 =C2=A01=
 pprocacci =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=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52 test2/dupdir2/dup1<br><div><br=
>And after the run of the perl script like so:<br>% ./<a href=3D"http://tes=
t.pl">test.pl</a> test1 test2<br><br>You want it to be like this:<br><br>% =
find test* -type f -ls<br>131318 =C2=A0 =C2=A0 =C2=A0 =C2=A09 -rwxr-xr-x =
=C2=A0 =C2=A01 pprocacci =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0pprocacci =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 763 May =
=C2=A05 06:58 <a href=3D"http://test.pl">test.pl</a><br>132503 =C2=A0 =C2=
=A0 =C2=A0 =C2=A09 -rw-r--r-- =C2=A0 =C2=A01 pprocacci =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0pprocacci =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 5 May =C2=A05 06:52 test1/dupdir2/dup1<br>1=
32898 =C2=A0 =C2=A0 =C2=A0 =C2=A09 -rw-r--r-- =C2=A0 =C2=A01 pprocacci =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0pprocacci =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 8 May =C2=A05 06:51 test1/dup=
dir1/dup<br>132771 =C2=A0 =C2=A0 =C2=A0 =C2=A09 -rw-r--r-- =C2=A0 =C2=A01 p=
procacci =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0pprocacci =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 4 May =C2=A05 06:5=
2 test2/dupdir1/dup<br><br></div><div>If so:<br><br>
####################################################

<br>#!/usr/bin/env perl<br><br>use strict;<br>use warnings;<br><br>sub msgD=
ie<br>{<br>=C2=A0 my ($ret) =3D shift;<br>=C2=A0 my ($msg) =3D shift // &qu=
ot;$0 dir_base dir\n&quot;;<br>=C2=A0 print $msg;<br>=C2=A0 exit($ret);<br>=
}<br><br>sub doit<br>{<br>=C2=A0 my($base, $cur) =3D @_;<br><br>=C2=A0 open=
dir(my $dh, $cur) or msgDie(&quot;Unable to open directory: $cur\n&quot;);<=
br>=C2=A0 while(readdir $dh)<br>=C2=A0 {<br>=C2=A0 =C2=A0 next if($_ eq &#3=
9;.&#39; || $_ eq &#39;..&#39;);<br>=C2=A0 =C2=A0 if(-d &quot;$cur/$_&quot;=
){ doit(&quot;$base/$_&quot;, &quot;$cur/$_&quot;); next; }<br>=C2=A0 =C2=
=A0 next if(! -f &quot;$base/$_&quot;);<br><br>=C2=A0 =C2=A0 my ($ref) =3D =
(stat(&quot;$base/$_&quot;))[7];<br>=C2=A0 =C2=A0 my ($src) =3D (stat(&quot=
;$cur/$_&quot;))[7];<br>=C2=A0 =C2=A0 unlink(&quot;$cur/$_&quot;) if($ref =
=3D=3D $src);<br>=C2=A0 }<br>}<br><br>msgDie(1) unless(scalar @ARGV eq 2);<=
br><br>my $base =3D $ARGV[0];<br>my $dir =C2=A0=3D $ARGV[1];<br><br>msgDie(=
1, &quot;base diretory doesn&#39;t exist\n&quot;) unless -d $base;<br>msgDi=
e(1, &quot;source diretory doesn&#39;t exist\n&quot;) unless -d $dir;<br><b=
r>doit($base, $dir);<br>###################################################=
#<br><br></div><div>~Paul<br></div><div><br><span class=3D"gmail_signature_=
prefix">-- </span><br><div dir=3D"ltr" class=3D"gmail_signature">__________=
________<br><br>:(){ :|:&amp; };:</div></div></div></div>

--0000000000009af6d205faecdbf6--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAFbbPug8npwyxjzxB20wV%2BmHpqovoCwPyx1p89GRSKDWhA_qcQ>