#!/usr/bin/perl use strict; use warnings; use Getopt::Long 'GetOptions'; use autouse 'Pod::Usage' => 'pod2usage'; GetOptions( nop => \ my($nop), verbose => \ my($verbose), help => sub { pod2usage( -verbose => 1 ) }, man => sub { pod2usage( -verbose => 2 ) } ) or die pod2usage( -verbose => 0 ); @ARGV == 2 or pod2usage( -verbose => 0 ); my ( $src, $tgt ) = @ARGV; -d $tgt and -d $src or pod2usage( -verbose => 0 ); =head1 NAME merge-move - Moves the contents of one directory into another by mergi +ng files. =head1 SYNOPSIS merge-move /home/data/blah /home/data/somewhere-else Options: --nop --verbose =head1 OPTIONS =over =item --nop Don't do any eral work. =item -verbose Show what's happening. =back =cut use autouse 'Cwd' => 'cwd'; use autouse 'File::Find' => 'find'; use autouse 'File::Spec::Functions' => qw( canonpath catfile splitpath + catdir ); use autouse 'File::Path' => 'mkpath'; use autouse 'File::Copy' => 'move'; $SIG{CHLD} = 'IGNORE'; my $pwd = cwd(); my $pwd_rx = qr/\A\Q$pwd/; my ( %dirs ); find( { wanted => sub { return if not -f $_; my $srcfile = canonpath( $File::Find::name ); my ( undef, $srcdir, $file ) = splitpath( $srcfile ); my $tgtdir = catdir( $tgt, $srcdir ); if ( ! exists $dirs{$tgtdir} and ! -d $tgtdir ) { $dirs{$tgtdir} = undef; if ( $verbose ) { print "mkdir $tgtdir\n"; } if ( not $nop ) { mkpath $tgtdir, 0, 0775 or die "Can't create $tgtdir: $!"; } } my $tgtfile = catfile( $tgtdir, $file ); if ( -e $tgtfile ) { die "Couldn't move $srcfile: $tgtfile already exists.\ +n"; } if ( $verbose ) { print "mv $srcfile $tgtfile\n"; } if ( not $nop ) { move $srcfile, $tgtfile or die "Couldn't move $srcfile to $tgtfile: $!"; } }, no_chdir => 1 }, $src );

In reply to Move/merge directories by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.