Search code examples
linuxbashshellhexdecimal

Convert hex file names to decim


I have several files named in a hex format. I'd like to mass rename them into their decimal equivalents, turning them from

  1. 0x000f750d.wav
  2. 0x000f750e.wav
  3. 0x000f750f.wav

to

  1. 1013005.wav
  2. 1013006.wav
  3. 1013007.wav

I've tried the following command in order to fetch the files' names in decimal and rename them, but I'm unsure how to go about it. (Note: before this command, I removed the .wav extensions from the files with the intent of readding them later to make it easier on myself.)

for f in *; do
    mv -- "$(($f))" $f;
done

This is the result:

mv: cannot stat '1013005': No such file or directory
mv: cannot stat '1013006': No such file or directory
mv: cannot stat '1013007': No such file or directory
mv: cannot stat '0': No such file or directory

Solution

  • You basically had it, but with the wrong order of arguments. This should work, and removes the extension for $((...)), then adds it back:

    for f in *; do mv -- "$f" "$((${f/%.wav})).wav"; done