9

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?

kenorb
  • 20,988
Ben
  • 225
  • 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: http://unix.stackexchange.com/questions/159253/decoding-url-encoding-percent-encoding?lq=1 (which to me seems pretty duplicate to this one in some way) – syntaxerror Sep 15 '15 at 22:21

5 Answers5

12

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 $_' *%*
Runium
  • 28,811
8

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*
don_crissti
  • 82,805
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
Chris Down
  • 125,559
  • 25
  • 270
  • 266
0

For some Linux users, the rename command in the the best answer won't work.

As described here, another non-Perl rename program is also available, which is provided as part of the util-linux project.

The syntax for the same command (for files starting with L) with the util-linux command is:

rename '%20' ' ' L*