Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert percent-encoded file URL to local file in bash

Tags:

bash

url

I have a percent encoded file URL - for example file:///home/sashoalm/Has%20Spaces.txt. I need to convert it to a local file - /home/sashoalm/Has Spaces.txt.

How can I do that in bash?

like image 574
sashoalm Avatar asked Dec 25 '22 01:12

sashoalm


1 Answers

In BASH you can use this utility function:

decodeURL() {
   printf "$(sed 's#^file://##;s/+/ /g;s/%\(..\)/\\x\1/g;' <<< "$@")\n";
}

Then call this function as:

decodeURL 'file:///home/sashoalm/Has%20Spaces.txt'
/home/sashoalm/Has Spaces.txt
like image 52
anubhava Avatar answered Feb 11 '23 19:02

anubhava