How can I decode a file name using command line?
The standard (POSIX/Unix) command to get the byte values as hex numbers is od
.
file=foo.mp3
printf %s "$file" | od -An -vtx1
Which gives an output similar to:
66 6f 6f 2e 6d 70 33
$file
above contains an arbitrary array of (non-NUL for shells other than zsh
) bytes. The character encoding doesn't enter in consideration.
If you want $file
to contain an array of characters (so in the locale's encoding) and you want to get the Unicode code points for each of them as hexadecimal numbers, on a Little-Endian system, you could do:
printf %s "$file" | iconv -t UTF-32LE | od -An -vtx4
See also:
printf %s "$file" | recode ..dump
Or:
printf %s "$file" | uconv -x hex/unicode
printf %s "$file" | uconv -x '([:Any:])>&hex/unicode($1)\n'
If you wanted the byte values as hex numbers of the UTF-8 encoding of those characters:
printf %s "$file" | iconv -t UTF-8 | od -An -vtx1
For something like foo.mp3
that contains only ASCII characters, they're all going to be equivalent.
With perl
:
$ perl -CA -le 'print join " ", map { sprintf "0x%X", $_ } unpack "U*" for @ARGV' \
foo.mp3 bar.mp3 cường
0x66 0x6F 0x6F 0x2E 0x6D 0x70 0x33
0x62 0x61 0x72 0x2E 0x6D 0x70 0x33
0x63 0x1B0 0x1EDD 0x6E 0x67
If you store those list of filenames in a file, then:
perl -CI -lne 'print join " ", map { sprintf "0x%X", $_ } unpack "U*"' <file
With plain Bash:
a=abcdefghij
for ((i=0;i<${#a};i++));do printf %02X \'${a:$i:1};done
6162636465666768696A
Customize the printf
format to suit your needs.