7

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?

  • My mistake, I tried searching but it must have evaded my efforts. The answers here are pretty useful and well explained though. – Ben May 20 '13 at 21:43
  • 3
    And the question is quite useful in itself when generalised to undo URI encoding. – Stéphane Chazelas May 26 '13 at 16:00
  • Also covered in the following question: unix.stackexchange.com/questions/159253/… (which to me seems pretty duplicate to this one in some way) – syntaxerror Sep 15 '15 at 22:21
9

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 $_' *%*
7

You can use deurlname from renameutils.

$ ls
L4%20Molecular%20Recognition.pdf
$ deurlname L4%20Molecular%20Recognition.pdf
$ ls
L4 Molecular Recognition.pdf

I wrote a script that allows you to rename files in an editor.

You just pass the script a filename and it opens your editor with the filename in it. Then you edit the filename, write, and close the editor.

$ ls
  L4%20Molecular%20Recognition.pdf
$ viname L4%20Molecular%20Recognition.pdf
  ======================
  L4%20Molecular%20Recognition.pdf█
  ======================

  ======================
  L4-Molecular-Recognition.pdf█
  ======================
  (pretend this is an editor)
$ ls
  L4-Molecular-Recognition.pdf

I also wrote a script that automatically renames files to conform to my preferred naming scheme. When I download files, the first thing I do is call this script on them.

$ ls
  L4%20Molecular%20Recognition.pdf
$ nf L4%20Molecular%20Recognition.pdf
  'L4%20Molecular%20Recognition.pdf' renamed to 'l4-molecular-recognition.pdf'
$ ls
  L4-molecular-recognition.pdf

Be careful with that script. It can do some rather dramatic renames. Use its dry run (nf -n) option before renaming any files.

4

You could use convmv in the directory where you have the files:
To test what the output would be:

convmv --unescape *%20*

To actually rename the files add --notest:

convmv --unescape --notest *%20*
4

Another alternative that doesn't rely on external tools outside of bash:

for old in *; do
    new="${old//+/ }"
    printf -v new '%b' "${new//%/\x}"
    mv -- "$old" "$new"
done
  • Great answer if you've come upon this page looking for a MacOS solution (like me). – Tim Mackey Nov 25 '18 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.