Using a loop in bash
:
for name in *.mp4 *.srt; do
mv -i -- "$name" "${name/-*./.}"
done
This renames each .mp4
file and .srt
file by replacing the part of the name between the first dash and the last dot with a dot, by means of a variable substitution.
I chose to pick out the .mp4
and .srt
files specifically, since these are the ones you show in the question.
Using a portable sh
loop:
for name in *.mp4 *.srt; do
mv -i -- "$name" "${name%%-*}.${name##*.}"
done
Here, ${name%%-*}
will be the original name with everything after the first dash cut off, and ${name##*.}
will be the filename suffix after the last dot in the filename.
Using the Perl rename
utility:
$ tree
.
|-- 01. file one-sdvanv-12lknl.mp4
|-- 01. file one-sdvanv-12lknl.srt
|-- 02. file two-afdsmakl-asdfafdaf.mp4
|-- 02. file two-afdsmakl-asdfafdaf.srt
|-- 03. file three-adfadaasd-asdfadfafad-adad1d1das.mp4
`-- 03. file three-adfadaasd-asdfadfafad-adad1d1das.srt
0 directory, 6 files
$ rename 's/-.*\././' -- *.mp4 *.srt
$ tree
.
|-- 01. file one.mp4
|-- 01. file one.srt
|-- 02. file two.mp4
|-- 02. file two.srt
|-- 03. file three.mp4
`-- 03. file three.srt
0 directory, 6 files
The Perl expression s/-.*\././
is a substitution that will be applied to each given filename, renaming the file.
This substitution replaces the part of the filename from the first dash to the last dot with a dot.
You may want to add -n
to the invocation of the rename
utility to first see what would happen.
See also:
file[space-here]
an actual part of filename itself? – DevilaN Apr 06 '20 at 12:53