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

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

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


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.


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!
  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (5)
    As of 2018-07-22 07:07 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (452 votes). Check out past polls.