Select Page
(you can read this publication in spanish here)

Useful one-liner to rename multiple files using regular expressions:

perl -we '$regex = shift(@ARGV); $rep = shift(@ARGV); $rep =~ s/"/\"/g; $rep = qq("$rep"); foreach (@ARGV) { if (/$regex/) { $ren = s/$regex/$rep/geer;  print qq(Renaming "$_"n      to "$ren"n); rename($_, $ren); }}' <pattern> <replacement> <files...>

Brought up because of the Programming Languages class at Coursera, whose videos are numbered without padding zeros, (p.e. “2 – 9 – Functions Formally (856).mp4”), messing up sort order in many programs (like VLC).

To fix it from our trusty Linux terminal, first we create an alias for neatness (which you can also save permanently in your ~/.bashrc):

alias renamregex='perl -we '''$regex = shift(@ARGV); $rep = shift(@ARGV); $rep =~ s/"/\"/g; $rep = qq("$rep"); foreach (@ARGV) { if (/$regex/) { $ren = s/$regex/$rep/geer;  print qq(Renaming "$_"n      to "$ren"n); rename($_, $ren); }}''

Then we change to the directory with the videos, replacing mine with your own of course (or you can simply press F4 in Dolphin to open a terminal in it, if you use KDE):

cd "~/Documentos/Educación/Cursos en línea/Coursera UoW - Programming Languages/"

And we add an underscore to the video length (so the video name is the same as the subtitle):

renamregex '(d)(dd).mp4)' '$1_$2' *.mp4

Renaming "2 - 9 - Functions Formally (856).mp4"       to "2 - 9 - Functions Formally (8_56).mp4"

And then we rename all files, adding a zero to all the lone digits, except the lastwe don’t want to change the extension:

renamregex '(^|D)(d)(D)' '${1}0$2$3' *

Renaming "2 - 9 - Functions Formally (8_56).mp4"      to "02 - 09 - Functions Formally (08_56).mp4"

As a goodie for using the best OS for developers, you can install all the programs required on the class with a single command (in Debian/Ubuntu and friends):
sudo apt-get install emacs24 smlnj sml-mode racket ruby libtcltk-ruby

Though SML mode requires a quick fix (to fix therequire: Constant symbol `:groupspecified in defvar” error):

sudo perl -pi -e 's/^(defvar :.*n//' /usr/share/emacs/site-lisp/sml-mode/sml-compat.el
sudo emacs -batch -f batch-byte-compile  /usr/share/emacs2*/site-lisp/sml-mode/sml-compat.el

If you want to use Emacs instead of DrRacket, install Quack and Geiser with:
sudo apt-get install emacs-goodies-el

and for the latest Geiser:
wget http://ubuntu.wikimedia.org/ubuntu/pool/universe/g/geiser/geiser_0.4-1_all.deb
sudo dpkg -i geiser_0.4-1_all.deb
rm geiser_0.4-1_all.deb

(or you can just apt-get install geiser if you’re already in Ubuntu Sausy or Debian Jessie).

And add to your ~/.emacs:

;; Improved scheme-mode for Racket
(require 'geiser-install)
(require 'quack)
(quack-install)

If you’re in Windows, you’ll have to install Perl and run something like:

cd "$HOMEMis DocumentosEducaciónCursos en líneaCoursera UoW - Programming Languages" 

perl -we '$regex = shift(@ARGV); $rep = shift(@ARGV); $rep =~ s/"/\"/g; $rep = qq("$rep"); foreach (@ARGV) { if (/$regex/) { $ren = s/$regex/$rep/geer;  print qq(Renaming "$_"n      to "$ren"n); rename($_, $ren); }}'
'(d)(dd).mp4)' '$1_$2' *.mp4

perl -we '$regex = shift(@ARGV); $rep = shift(@ARGV); $rep =~ s/"/\"/g; $rep = qq("$rep"); foreach (@ARGV) { if (/$regex/) { $ren = s/$regex/$rep/geer;  print qq(Renaming "$_"n      to "$ren"n); rename($_, $ren); }}' '(^|D)(d)(D)' '${1}0$2$3' *.*

Though I haven’t tried it.