Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Syntactic Confectionary Delight
 
PerlMonks

How to rename to lowercase every file in a directory and its subdirectories?

 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

( #131861=categorized question: print w/ replies, xml ) Need Help??
Contributed by JoeCool on Dec 14, 2001 at 05:08 UTC
Q&A  > directories


Description:

    I recently D/L a bunch of file and instead of renaming every file in the directory and sub directory manually. I have tried to input a dir and rename all files in that and sub to lc... Please Advise, Joe

Answer: How do u rename every file in a dir and sub?
contributed by rob_au

This code performs this task for you - It makes use of File::Find to find entries within the given path, ensures that they are actually files (stat and -f _) and then renames them. This could be made more precise by added exclusion matches. eg. return unless (m/\.mp3$/);

#!d:/perl/bin/perl -w use File::Find; use strict; find ({ 'wanted' => \&renamefile, }, '/path/to/sub/directory'); sub renamefile { my ($dev, $ino, $mode, $nlink, $uid, $gid); return unless (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_) +); return unless (-f _); rename $_, lc($_); }
Answer: How do u rename every file in a dir and sub?
contributed by robin

If you're using a Unix system (e.g. Linux or Solaris) then the easiest way is not entirely Perl, but uses the find command to get the filenames, and Perl for the actual renaming:

find . -type f -exec \ perl -e 'rename($_, lc) || warn "$_: $!\n" for @ARGV' {} \;

That turns all the filenames in the current directory and below to lower case. If you're using a system which doesn't have the find command, you can use the find2perl utility (it comes with Perl) to generate a Perl script which will behave the same way. On Windows you can type:

find2perl . -type f -eval "rename $_, lc or warn qq{$_: $!\n}"

and then run the script that it outputs.

Answer: How do u rename every file in a dir and sub?
contributed by JPaul

One way to do it would be something like this:

#!/usr/bin/perl -w use strict; # Open 'find' process to list files recursively with paths open(FIND, "find |"); while(<FIND>) { chomp; next if $_ eq $0; # Don't rename ourself rename($_, lc($_)); } close(FIND);
It's not remarkably efficient, but it'll do your bidding.

JP

Answer: How to rename to lowercase every file in a directory and its subdirectories?
contributed by Alex the Serb

you have to use recursion, because you dont know how much deep all subdirectories goes

#!/usr/bin/perl -w use strict; my $path_to_dir = shift; dir("$path_to_dir"); sub dir { opendir(DIR,"$_[0]"); my @list_of_files = readdir(DIR); foreach(@list_of_files) { if($_ ne "." && $_ ne "..") { if(-d "$_[0]/$_") { dir("$_[0]/$_"); } else { rename "$_[0]/$_","$_[0]/"."\L$_"; } } } }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Login:
    Password
    remember me
    What's my password?
    Create A New User

    Community Ads
    Chatterbox
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users
    Others perusing the Monastery: (7)
    GrandFather
    wfsp
    atcroft
    herveus
    Eyck
    gnosti
    im2
    As of 2009-11-21 09:29 GMT
    Sections
    The Monastery Gates
    Seekers of Perl Wisdom
    Meditations
    PerlMonks Discussion
    Categorized Q&A
    Tutorials
    Obfuscated Code
    Perl Poetry
    Cool Uses for Perl
    Perl News
    Information
    PerlMonks FAQ
    Guide to the Monastery
    What's New at PerlMonks
    Voting/Experience System
    Tutorials
    Reviews
    Library
    Perl FAQs
    Other Info Sources
    Find Nodes
    Nodes You Wrote
    Super Search
    List Nodes By Users
    Newest Nodes
    Recently Active Threads
    Selected Best Nodes
    Best Nodes
    Worst Nodes
    Saints in our Book
    Leftovers
    The St. Larry Wall Shrine
    Offering Plate
    Awards
    Craft
    Snippets Section
    Code Catacombs
    Quests
    Editor Requests
    Buy PerlMonks Gear
    PerlMonks Merchandise
    Planet Perl
    Perlsphere
    Use Perl
    Perl.com
    Perl 5 Wiki
    Perl Jobs
    Perl Mongers
    Perl Directory
    Perl documentation
    CPAN
    Random Node
    Voting Booth

    Future historians will find that the material characteristic of the current era is...

    Aluminium
    Plastic
    Oil
    Water
    Carbon dioxide
    Copper
    Iron
    Silicon
    Salt
    Uranium
    Hydrogen
    Other

    Results (729 votes), past polls