Sunday, December 15, 2013

Popular Rename Technics

 Popular Batch Rename Technics in Linux

- using command :

  for f in *; do mv -v "$f" "{prefix}$f"; done

  for f in *; do mv -v "$f" "$f{suffix}"; done

 using rename .pl

 rename "expression1" "expression2" *.ext 

  ex: rename file1.mp3, file2.mp3...... to 1.mp3,2.mp3......

rename "file" ""  *.mp3


- using php

limit the filename to 20 characters

if ($handle = opendir('/path/to/files')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = substr($fileName,0,20);
        rename($fileName, $newName);
    }
    closedir($handle);
}


delete the last 4 characters of filename for all files



if ($handle = opendir('/path/to/files')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = substr($fileName,0,-4);
        rename($fileName, $newName);
    }
    closedir($handle);
}


rename all .ext files eg: rename all .mp3 files to song_*.mp3

if ($handle = opendir('./')) {
    while (false !== ($fileName = readdir($handle))) {
  if ( ($ext = end(explode('.', $fileName)) == "mp3") || ($ext = end(explode('.', $fileName)) == "MP3")){
      $newName = 'song_'.$fileName;
        rename($fileName, $newName);
}
    }
    closedir($handle);
}




rename when match expression
eg : rename video12.mp4, video589.flv, file17.mp3 to video12_old.mp4, video589_old.flv, file17.mp3


if ($handle = opendir('./')) {
    while (false !== ($fileName = readdir($handle))) {
  if ( preg_match('/^video/',$fileName)){
    $newName = $fileName.'_old';
        rename($fileName, $newName);
   
}
    }
    closedir($handle);
}


 





 

 

1 comments:

  1. Thanks a lot for sharing this with all folks you really recognise what you are talking about! In this complex environment business need to present there company data in meaningful way.So user easily understand it .Sqiar (http://www.sqiar.com/) which is in UK,provide services like Tableau and Data Warehousing etc .In these services sqiar experts convert company data into meaningful way.

    ReplyDelete