I have a directory with files, whose names I need to standardize.
The names like audio_32.mp4.mp4
I would like to change to audio_0-32.mp4
. That is, I would like to add a zero after the underscore and introduce a hyphen afterwards. The files differ by the number in their name. That is, there will also be a file named audio_33.mp4.mp4
and so on. I would like to leave all other files and patterns unchanged. It is fine if removing the tail .mp4
and adding the zero are dealt with as separate tasks.
How can I do this efficiently?
CodePudding user response:
Which operating system will your code be run on? There are already a lot of tools for this sort of thing that exist, so you're entering re-invent the wheel territory here.
Powertoys PowerRename for example is designed for exactly this.
That said, if I assume the availability of the typical GNU toolset you can get rid of the mp4 with "basename":
[user@host]$ basename audio_33.mp4.mp4 .mp4
audio_33.mp4
[user@host]$
The rest you can do with basic string manipulation:
[user@host]$ X=audio_33.mp4
[user@host]$ echo ${X/audio_/audio_0-}
audio_0-33.mp4
[user@host]$
CodePudding user response:
Doing this in pure bash:
for f in *.mp4; do
t="${f%.mp4}"
echo mv "$f" "${t/_/_0-}"
done
Once you're satisfied with the output, remove echo
before mv
to rename your files instead of printing mv
command.