How to create a script to rename files on Ubuntu 18?

Asked

Viewed 454 times

2

I have a folder called baixar-nanatsu and inside the folder I have several videos. I wanted to change the name of the videos via bash or shell script automatically, only I have no idea how it does.

Contents of the folder baixar-nanatsu:

nanatsu-ep-1.mp4
nanatsu-ep-2.mp4
nanatsu-ep-3.mp4
nanatsu-ep-4.mp4

I would like to change so that:

1.mp4, 2.mp4, 3.mp4 etc

2 answers

4


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>...

  • 1

    Really helped a lot. I will study a little more about it there to improve my renames. Thanks.

1

Normally under Linux there is (or can be installed) a command Name, or name that helps with these tasks:

prename 's/nanatsu-ep-//' nanatsu-ep-*.mp4

That is to say:

  • for all the nanatsu-ep-*.mp4
  • s/nanatsu-ep-// replaces nanatsu-ep- for nothing. (It can be any Perl statement that changes the name)

Similarly, using the rename:

rename nanatsu-ep- "" nanatsu-ep-*.mp4

Browser other questions tagged

You are not signed in. Login or sign up in order to post.