How to remove URI encoding from file names?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

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*

Method 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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x