pi3142 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am trying to rename multiple files at once (in UNIX). For example: From /folder1/abc.txt /folder2/abc.txt /folder3/abc.txt . . . /folder100/abc.txt to /folder1/folder1.txt /folder2/folder2.txt /folder3/folder3.txt . . . /folder100/folder100.txt Is there a quick way to do it? Thanks so much!
  • Comment on Rename multiple files following their directories' name

Replies are listed 'Best First'.
Re: Rename multiple files following their directories' name
by choroba (Cardinal) on Mar 19, 2013 at 11:36 UTC
    You can probably use a shell for this task:
    for folder in folder* ; do [ -f $folder/abc.txt ] && mv $folder/abc.txt $folder/$folder.txt done

    In Perl, the solution is quite similar:

    for my $folder (glob 'folder*') { # or (grep -d, glob 'folder*') rename "$folder/abc.txt", "$folder/$folder.txt" if -f "$folder/abc +.txt"; }

    (Untested)

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Rename multiple files following their directories' name
by hdb (Monsignor) on Mar 19, 2013 at 11:32 UTC
    Are you looking for something like?
    for $i (1..100) { system( "mv /folder$i/abc.txt /folder$i/folder$i.txt" ); }
    I run under win7, so I have not tested it. If you want to search for files you need File::Find.
    For every complex problem there is an answer that is clear, simple, and wrong. H. L. Mencken