Wednesday, December 10, 2008

Bash one-liner: batch rename

Here is a bash recipe to rename files in batch. It can also be used to retag mp3 files according to their file names.
for a in *; do b=`echo $a|sed 's/.*\([0-9][0-9]*\).*\(\..*\)$/\1\2/' `; mv "$a" "$b"; done
In this example, all files are renamed to the first collection of numbers in its original names, while keeping the original postfix. E.g.,
blue4.mp3 -> 4.mp3
11help.dat -> 11.dat

No comments:

 
Creative Commons License All contents on this page are licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.