Convert percent-encoded file URL to local file in bash Convert percent-encoded file URL to local file in bash bash bash

Convert percent-encoded file URL to local file in bash


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


Urlencode should do it (-d option)

-d Do URL-decoding rather than encoding, according to RFC 1738. %HH and %hh strings are converted and other characters are passed through unmodified, with the exception that + is converted to space.


Perl to the rescue:

echo file:///home/sashoalm/Has%20Spaces.txt |\  perl -MURI -MURI::Escape -lne \      'print uri_unescape(URI->new($_)->path)'

See URI and URI::Escape.