Command Line Rename – How to Remove URI Encoding from File Names

command linerename

I realise this might be quite a simple question, but I'm still quite new to the command line and only have a grasp of basic commands.

I've downloaded some lecture presentations (~25 or so) from my University however on doing so they've been named things like…

L2%20Development%20of%20immune%20system.pptx
L4%20Molecular%20Recognition.pdf

As you can see they've downloaded with the URL encoding %20 instead of a space.

My question is how to batch rename all of these files so that the %20 is removed and replaced with a space instead?

Best Answer

On Debian and derivatives (including Ubuntu), you could use rename, which applies a Perl expression to each file name.

rename 's/%20/ /g' L*
        |  |  | |   |
        |  |  | |   +--- Files to match
        |  |  | +------- globally
        |  |  +--------- with space
        |  +------------ %20
        +--------------- Substitute

I would consider using underscore instead of space – as it generally makes life easier in the cli world.

To generalise to all URI encoding:

rename 'use URI::Escape; $_ = uri_unescape $_' *%*
Related Question