Just make a for
in the files and use the command mv
to rename them:
for i in nanatsu*.mp4; do mv $i ${i#nanatsu-ep-}; done
The for
goes through all the files whose names correspond to nanatsu*.mp4
(that is, they start with "Nanatsu" and end with ". mp4"). At each iteration the variable i
will have the name of one of the files.
Then the command mv
receives the current file name and the new name it will have. The current name is $i
, that is, the current file name.
The new name is ${i#nanatsu-ep-}
, which uses the syntax of bash substitution (see item 4 of this tutorial for more details). Basically it takes the value of i
and remove the excerpt nanatsu-ep-
, leaving only the final part (the number followed by ". mp4").
At the end, the files are renamed the way you need them (nanatsu-ep-1.mp4
flipped 1.mp4
, nanatsu-ep-2.mp4
flipped 2.mp4
, etc.).
Remembering that you must be inside the folder, because the for
above takes the files from the current folder. Then it would be:
cd baixar-nanatsu
for i in nanatsu*.mp4; do mv $i ${i#nanatsu-ep-}; done
But if you want to run from anywhere, you can pass the complete file path: baixar-nanatsu/nanatsu*.mp4
. But the command gets a little more complicated, since the bash substitution only removes the start or end snippets of the variable:
for i in baixar-nanatsu/nanatsu*.mp4
do
file=$(basename $i)
dir=$(dirname $i)
mv $i ${dir}/${file#nanatsu-ep-}
done
As now the i
will have the value baixar-nanatsu/nome-do-arquivo.mp4
, i use basename
to take only the file name (without the folder) and dirname
to get the name of the directory. To get the output of these commands I use the syntax of command substitution (put the command between $(...)
).
Next I use these values to compose the new file name (the folder is the same, and the name uses the bash substitution, in the same way as before.
For more complex names, as reported in your comment, just use the replacement with %
to remove excerpts from the end along with #
to remove snippets from the beginning:
# se o nome for The.Seven.Deadly.Sins.S01E02.720p.WEB-DL.DUBLADO.WWW.COMANDOTORRENTS.COM.mkv
for i in *
do
tmp=${i#The.Seven.Deadly.Sins.S01E}
mv $i ${tmp%720p.WEB-DL.DUBLADO.WWW.COMANDOTORRENTS.COM.mkv}mkv
done
First I remove the section #The.Seven.Deadly.Sins.S01E
, and then remove the rest, leaving only the number 02 (and add the extension at the end again).
I read this tutorial here, but I couldn’t really do what I wanted... The videos I have are <code>The.Seven.Deadly.Sins.S01E02.720p.WEB-DL.DUBLADO.WWW.COMANDOTORENTS.COM.mkv</code>, I managed to leave <code>03.720p.WEB-DL.DUBLADO.WWW.COMANDOTORENTS.COM.mkv</code> but I couldn’t get what comes after <code>03</code code>...
– Sandson Costa
Really helped a lot. I will study a little more about it there to improve my renames. Thanks.
– Sandson Costa