Ubuntu: How to replace the %-encoded chars in a bunch of filenames by underscore using shell commands?


I downloaded lots of files, some of which containing invalid chars encoded to something like %2C, %2F...

I tried rename 's/%[0-9][A-Z]/_/g * and rename 's/\%[0-9][A-Z]/_/g * but they do nothing. (Sorry, my fault. rename works. I input some typos or something related to fullwidth/halfwidth chars... ONZ.......... Plz vote to close this Q)

For example, the filenames file,1.pdf, file/2.pdf ... are encoded as file%2C1.pdf, file%2F2.pdf. I want to rename them to file_1.pdf, file_2.pdf,...


You can use:

rename 's/%/_/g' *  

First run it using -n option (dry run) to make sure everything is fine:

rename -n 's/%/_/g' *  

To capture the number too:

rename -n 's/%[0-9]/_/g' *  

or even use something like this to remove all characters except alpha-numeric ones:

$ rename -n 's/[^a-zA-z0-9]/_/g' *  rename(%SOME%2FFOR%, _SOME_2FFOR_)  rename(SOME%2FFOR, SOME_2FFOR)  rename(SOME%2FFOR%, SOME_2FFOR_)  


use the following script, copy the script and save it in /bin as renamer.sh then anytime you want to do any renamings on files of this type, use the simple command renamer.sh #Input files#;

#!/bin/bash    for input in $@;      do           inputMod=$( echo "$input" | sed 's/\(%\)/\_/pg' );          mv "$input" "$inputMod" &> /dev/null;           shift;       done;  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »