I would change the approach from your script (sending a shell command loop to the remote side) to a three-step approach:

  1. Get the list of all remote files
  2. Munge the list in Perl
  3. Send a list of mv -i commands to the remote side

Using the three steps makes it much, much easier to review the list of commands before they are executed.

The relevant parts of the script would be:

# Get list of remote files my @remote_files= qx(ssh $uid $h 'find $dirname');
# Munge filenames to commands: my @commands= map { sprintf "mv -i '%s' '%s.bak'", quotemeta($_), quotemeta($_) } @remote_files;
# Send list of commands to the remote side: # First, a dry-run instead of actually doing that: my $remote= \*STDOUT; # Use this to actually do the remote execution: #open $remote, "| ssh -q -l $uid $h"; print { $remote } join "\n", @commands;

In reply to Re: Rename all files on remote server to *.bak recursively by Corion
in thread Rename all files on remote server to *.bak recursively by nancylt723

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.