I've got dozens of files such as:
title.mkv]-[72+167] - "" yEnc 637005001 (1+889)
I'd like to remove everything after ".mkv"
I tried looking up some regex that would help me, but I don't understand how to use them.
Could somebody help me?
I've got dozens of files such as:
title.mkv]-[72+167] - "" yEnc 637005001 (1+889)
I'd like to remove everything after ".mkv"
I tried looking up some regex that would help me, but I don't understand how to use them.
Could somebody help me?
Note on rename
, prename
etc. see:
In this answer the Perl variant is used, not the one form util-linux. As in not:
$ rename --version
rename from util-linux 2.31.1
but:
$ rename --version
/usr/bin/rename using File::Rename version 0.20
or similar.
With Perl rename:
rename 's/\.mkv.*/.mkv/s' ./*.mkv*
Use the -n
option first to make sure it does what you want.
rename -n 's/\.mkv.*/.mkv/s' ./*.mkv*
In short:
s/\.mkv.*/.mkv/s
is:
substitute/<THIS>/<WITH THIS>/s
\.mkv.*
matches a literal dot \.
then the string mkv
and finally any character (including \n
for which you need the s
flag) zero or more times .*
.
Result is replace .mkv<anything>
with .mkv
.
There is tools to help with reading and building expressions, like https://regex101.com/
With zsh
's zmv
:
autoload zmv
zmv -v '(*.mkv)*' '$1'
Removes the part after the rightmost occurrence of .mkv
in the filename (renames foo.mkv-bar.mkv-whatever
to foo.mkv-bar.mkv
).
For the leftmost occurrence (to rename that file to foo.mkv
instead), you could replace *.mkv
with *.mkv~*.mkv?*
which matches a string ending in .mkv
but which also otherwise does not contain .mkv
followed by at least one character using the ~
"except" glob operator.
in bash
:
for f in *; do [[ "$f" =~ ^.+\.mkv ]] && mv "$f" "${BASH_REMATCH[0]}"; done
In a script bash
#!/bin/bash
for i in *.mkv*
do
j=$(echo "$i" | sed -e 's/\.mkv.*$/.mkv/')
mv -- "$i" "$j"
done