http://www.perlmonks.org?node_id=11128792


in reply to Re: Move a file to another directory based on regex match
in thread Move a file to another directory based on regex match

I used /tmp as $OutDir .But still the same error "move failed: Invalid cross-device link"

  • Comment on Re^2: Move a file to another directory based on regex match

Replies are listed 'Best First'.
Re^3: Move a file to another directory based on regex match
by hippo (Bishop) on Feb 25, 2021 at 17:22 UTC

    How odd. Try this SSCCE which works fine for me.

    #!/usr/bin/env perl use strict; use warnings; use File::Copy 'move'; my $indir = '/tmp/foo'; my $outdir = '/tmp/bar'; mkdir $indir; mkdir $outdir; for my $f (qw/a b c/) { open my $out, '>', "$indir/$f" or die "Opening $f: $!"; print $out 'Woo-hoo!'; close $out; } opendir my $dir, $indir or die "Bad Dir $indir: $!"; while (my $infile = readdir ($dir)) { if ($infile =~ /b/) { print "Moving '$infile' ...\n"; move ("$indir/$infile", $outdir) or die "move failed: $! moving $indir/$infile to $outdir" +. " while we are in $ENV{PWD}\n"; } else { print "Skipping '$infile' ...\n"; } }

    🦛

      Thank you very much!!.It worked.