Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Rename multiple files following their directories' name

by pi3142 (Initiate)
on Mar 19, 2013 at 11:23 UTC ( #1024270=perlquestion: print w/ replies, xml ) Need Help??
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
Re: Rename multiple files following their directories' name
by hdb (Parson) 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
Re: Rename multiple files following their directories' name
by choroba (Abbot) 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)

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024270]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-07-25 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (172 votes), past polls