Batch Rename

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JCinDE
    Junior Member
    • Apr 2004
    • 18

    #1

    Batch Rename

    I'm trying to rename a batch of files to strip out numerics. I found many references online to using the "rename" command along with a perl regular expression to do this, but it seems the rename command available to us is different and doesn't support that.

    So anyone have a suggestion to quickly rename a batch of files using a regular expression?
  • ZYV
    Senior Member
    • Sep 2005
    • 315

    #2
    Well, I can share my PHP-script I use to organize my music collection, it is obviousely not the most straightforward way to go, but well, if works fairly well and I don't see a reason to write stuff in a language I poorly know (Perl / awk) instead of something I am very familiar with. Save this as "rename.php", run "chmod 755 rename.php" and then "./rename.php":

    Code:
    <?php
    $dir = './';
    $what = array("1", "2", "3", ); // etc.
    $with = "";
    
      $files = list_dir($dir);
      sort($files);
    
      foreach ($files as $file) {
    
         $name = str_replace($what, $with, $file);
    
         $newname = $dir . $name;
         $filename = $dir . $file;
    
         rename($filename, $newname);
    
         echo  $filename . " -> " . $newname ."\r\n";
      }
    
    
    function list_dir($dirname) {
        $result_array = array();
        $handle=opendir($dirname);
      while ($file = readdir($handle)) {
        if(($file=='.') || ($file=='..'))
          continue;
          $result_array[]=$file;
        }
      closedir($handle);
      return $result_array;
    }
    
    ?>
    The code is 5 years old so of course, now that can be done with glob() and friends. But it should work.

    Comment

    • Frank Hagan
      Senior Member
      • Mar 2004
      • 724

      #3
      I think you can use the mv command to rename files in a shell script. I found some examples using characters, but not numbers. You might test this with 0-9:

      Code:
      To rename with wildcards, you need to write a simple shell script. This can be done with any shell, but shells like ksh make it easier. For example, here's a script to rename a bunch of uppercase file names to lowercase:
      
      
       # [A-Z]* matches upper case names
       for i in [A-Z]*
       do
              j=`echo $i | tr '[A-Z]' '[a-z]'`
              mv $i $j
       done
      I think you'll want to add the "no-clobber" option to prevent overwriting any file while you're testing. I think its -i (but check mv --help from the shell).

      You can type that script into the SSH shell from the command line ... the "for" tells the shell to wait for the "done" to start processing the commands in order.

      Comment

      • ZYV
        Senior Member
        • Sep 2005
        • 315

        #4
        Yep, I think j=`echo $i | tr -d '[:digit:]'` should do the trick. I was just too lazy to Google how that could be done in bash, because I already had this PHP stuff on hand.

        Comment

        • Frank Hagan
          Senior Member
          • Mar 2004
          • 724

          #5
          I always passed over the "bash scripts" thinking I couldn't use them; I'm such a linux noob! Then I read you can just enter the commands at the prompt while you are logged in with shell access. Sure made my life easier!

          Comment

          Working...