Linux urldecode filename

Is there any tool to urldecode a file's name and replace it?

Example:

$ ls
hello%20world.txt
$ urldecode *.txt
$ ls
hello world.txt

Solution 1:

A new blog post covers this with echo(1) and printf(1).

urldecode() {
  arg="$1"
  i="0"
  while [ "$i" -lt ${#arg} ]; do
    c0=${arg:$i:1}
    if [ "x$c0" = "x%" ]; then
      c1=${arg:$((i+1)):1}
      c2=${arg:$((i+2)):1}
      printf "\x$c1$c2"
      i=$((i+3))
    else
      echo -n "$c0"
      i=$((i+1))
    fi
  done
}

Solution 2:

sed and echo can urldecode a file's name like so:

$ echo -e "$(echo hello%20world+ok | sed 's/+/ /g;s/%\(..\)/\\x\1/g;s/\\x\(2[Ff]\)/%\1/g')"
hello world ok

We want this to be able to turn filenames into filenames, rather than pathnames (i.e., we don't want to add slashes), so we leave %2F alone.  (To be more precise, we turn \x2F back into %2F.)

$ echo -e "$(echo cat+dog%20foo%21%22%23%24%25%26%27%28%29%2A%2B%2C%2D%2E%2Fbar |
                                    sed 's/+/ /g;s/%\(..\)/\\x\1/g;s/\\x\(2[Ff]\)/%\1/g')"
cat dog foo!"#$%&'()*+,-.%2Fbar

To actually rename files in the current directory:

$ ls *.txt
hello%20%20world++ok?.txt
$ for f in *.txt; do 
>   mv "$f" "$(echo -e "$(echo "$f" | sed 's/+/ /g;s/%\(..\)/\\x\1/g;s/\\x\(2[Ff]\)/%\1/g')")"
> done
$ ls *.txt
hello  world  ok?.txt

If the original (input) filenames contain backslashes, they will probably get mangled.