Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

4 Answers

up vote 2 down vote accepted

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 $_' *%*
share|improve this answer
add comment

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*
share|improve this answer
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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