Alejandro Alcalde

Data Scientist and Computer Scientist. Creator of this blog.

Alejandro Alcalde's posts | Porfolio

Note: Thanks to Cristina H for translating this post to english.

Sometimes when we get some files from the internet (usually music), each file contains in its name the name of the webpage from which it was downloaded.

If we want to rename all files to delete the web page from the file name, it is not necessary to rename files one by one, using the rename command from GNU/Linux we can get it done easily.

For example, lets say the files are as follows:

[email protected]:~/Desktop/RAP/d$ ls
01. Rafael Lechowski (Flowklorikos) - Uno -
02. Rafael Lechowski (Flowklorikos) - Lluvia y fuego -
03. Rafael Lechowski (Flowklorikos) - Donde duele inspira -
04. Rafael Lechowski (Flowklorikos) - Por amor al odio (con Gregory Isaacs) -
05. Rafael Lechowski (Flowklorikos) - Soy loco por ti -
06. Rafael Lechowski (Flowklorikos) - Desde el barro (con Carlos Talavera) -
07. Rafael Lechowski (Flowklorikos) - Folio en blanco (Improvisacion) -
08. Rafael Lechowski (Flowklorikos) - In-extremis -
09. Rafael Lechowski (Flowklorikos) - Sucio (con Carlos Talavera) -
10. Rafael Lechowski (Flowklorikos) - Artesano del arte insano -
11. Rafael Lechowski (Flowklorikos) - Cosquijazz -
12. Rafael Lechowski (Flowklorikos) - Mis ego depresiones -
13. Rafael Lechowski (Flowklorikos) - 13 -

We want to delete the following from their names: Rafael Lechowski (Flowklorikos) and For that we use the rename command with the following syntax:

rename 's/Rafael Lechowski (Flowklorikos) - //g' *.mp3

What we are saying with that expression is that we want to replace all that match with Rafael Lechowski (Flowklorikos) by nothing, that is, to delete it from the name. It is necessary to scape blanks and parenthesis. The *.mp3 means that we want to apply that renaming to all mp3 files.

As a result, now the files have the following names:

[email protected]:~/Desktop/RAP/d$ ls -1
01. Uno -
02. Lluvia y fuego -
03. Donde duele inspira -
04. Por amor al odio (con Gregory Isaacs) -
05. Soy loco por ti -
06. Desde el barro (con Carlos Talavera) -
07. Folio en blanco (Improvisacion) -
08. In-extremis -
09. Sucio (con Carlos Talavera) -
10. Artesano del arte insano -
11. Cosquijazz -
12. Mis ego depresiones -
13. 13 -

The only missing thing, is to delete the part. We get it done with the following command:

rename 's/ -' *.mp3

And finally, we have all our files renamed:

[email protected]:~/Desktop/RAP/d$ ls -1
01. Uno.mp3
02. Lluvia y fuego.mp3
03. Donde duele inspira.mp3
04. Por amor al odio (con Gregory Isaacs).mp3
05. Soy loco por ti.mp3
06. Desde el barro (con Carlos Talavera).mp3
07. Folio en blanco (Improvisacion).mp3
08. In-extremis.mp3
09. Sucio (con Carlos Talavera).mp3
10. Artesano del arte insano.mp3
11. Cosquijazz.mp3
12. Mis ego depresiones.mp3
13. 13.mp3

Spot a typo?: Help me fix it by contacting me or commenting below!


Maybe this posts are also worth reading