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?

3
  • My mistake, I tried searching but it must have evaded my efforts. The answers here are pretty useful and well explained though.
    – Ben
    Commented May 20, 2013 at 21:43
  • 3
    And the question is quite useful in itself when generalised to undo URI encoding. Commented May 26, 2013 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) Commented Sep 15, 2015 at 22:21

5 Answers 5

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 $_' *%*
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*
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
1
  • Great answer if you've come upon this page looking for a MacOS solution (like me).
    – Tim Mackey
    Commented Nov 25, 2018 at 3:59
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*

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .