Wednesday, December 14, 2011

Mass Rename Script

I need to convert bunch of files which has the same patterns and i need to do it quick, so i just created a simple bash script to help me out
#!/bin/bash

LS=`ls *`
for FILE in $LS
do
name=`ls $FILE | cut -d . -f1`
name1=`ls $FILE | cut -d . -f3`
name2=`ls $FILE | cut -d . -f4`
mv $FILE "$name.$name1.$name2"
done
So, if i have a filename with a.xxx.01.avi and i wanted to remove the xxx part, i only need to execute the script and every file with that pattern will be changed to a.01.avi.

Simple huh? big grin BASH Rock

4 comments:

  1. Thanks for sharing... never used bash yet...

    ReplyDelete
  2. Anonymous9:36 PM

    Nice work, but also check out the rename command...not sure if it could have done what you wanted, but it's pretty good!

    [grdryn@localhost ~]$ rename
    rename: not enough arguments

    Usage:
    rename [options] expression replacement file...

    Options:
    -v, --verbose explain what is being done
    -V, --version output version information and exit
    -h, --help display this help and exit

    ReplyDelete
  3. Yes, that's also possible, but for lots of files, we still need scripting :)

    ReplyDelete
  4. Anonymous1:14 AM

    rename 's/\.xxx//g' *avi

    Would do the same.

    ReplyDelete