Bulk renaming files so they are more web friendly

I needed a quick solution to make .docx and pdf‘s more “web friendly“. Therefore I created this quick Perl one-liner below to take care of the deed 🙂

perl -M'File::Copy qw~move~' -sle 'map { map { $o=$_; s`[\s_-](?{ $m = q~-~ })|[!-,:-\@\[-^\`\/](?{ $m = q~~ })`$m`ge; $_ = "\L$_\E"; print qq~$o --> $_~; move $o, $_ } glob (qq~*.${_}~) } split (m~,~, $f)' -- -f='docx,pdf'

Note

  • You can change -f='doc,pdf' to any extensions you like!

.bashrc

For convenience you can make this a function and put it in your .bashrc

function webpal()
{
	perl -M'File::Copy qw~move~' -sle 'map { map { $o=$_; s`[\s_-](?{ $m = q~-~ })|[!-,:-\@\[-^\`\/](?{ $m = q~~ })`$m`ge; $_ = "\L$_\E"; print qq~$o --> $_~; move $o, $_ } glob (qq~*.${_}~) } split (m~,~, $f)' -- -f=$1
}

Syntax

You can invoke the bash function with webpal “extension1,extension2”

Leave a Reply

Your email address will not be published. Required fields are marked *