LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Reverse file name (https://www.linuxquestions.org/questions/linux-newbie-8/reverse-file-name-4175638000/)

zetrotrack000 09-08-2018 05:48 AM

Reverse file name
 
Hi
I have many songs in my music folder. Previously, I used to save my music files as:
"song name - singer name.mp3"
for example: "the way i are - timbaland.mp3"
But now I want to reverse this, i.e
"singer name - song name.mp3"
like: "timbaland - the way i are.mp3"
So, do I have to do this manually for every song or can it be done by any command or script?
Thanks

Turbocapitalist 09-08-2018 05:55 AM

There is a utility called rename which may be in your distro by defailt. It can take any perl expression and use it to modify the file name automatically:

Code:

rename -n 's/^([^-]+) - ([^-]+).mp3/$2 - $1.mp3$/' *.mp3
The -n is for a dry-run. So when you have things the way you like, remove the -n and it will actually change the names.

zetrotrack000 09-08-2018 05:58 AM

Quote:

Originally Posted by Turbocapitalist (Post 5901150)
There is a utility called rename which may be in your distro by defailt. It can take any perl expression and use it to modify the file name automatically:

Code:

rename -n 's/^([^-]+) - ([^-]+).mp3/$2 - $1.mp3$/' *.mp3
The -n is for a dry-run. So when you have things the way you like, remove the -n and it will actually change the names.

Can you please give me an example by using the names which I have used in my original post?

syg00 09-08-2018 06:03 AM

Turbocapitalist has given you that answer - try it and see. The regex is (hopefully) generic enough to handle all your files - unless you haven't been specific enough.
Running the command will do no damage, but might be informative.

zetrotrack000 09-08-2018 06:04 AM

1 Attachment(s)
Quote:

Originally Posted by syg00 (Post 5901157)
Turbocapitalist has given you that answer - try it and see. The regex is (hopefully) generic enough to handle all your files - unless you haven't been specific enough.
Running the command will do no damage, but might be informative.

I ran (with and without -n) and nothing happened. Please check attached image.

AnanthaP 09-08-2018 06:26 AM

The mp3 within the brackets is mp3$

zetrotrack000 09-08-2018 06:35 AM

1 Attachment(s)
Quote:

Originally Posted by AnanthaP (Post 5901164)
The mp3 within the brackets is mp3$

Still not working. Please see attached image.
PS: I also tried by putting $ on both mp3

Turbocapitalist 09-08-2018 06:47 AM

Which distro do you have, including version?

Code:

lsb_release -rd

zetrotrack000 09-08-2018 06:48 AM

Quote:

Originally Posted by Turbocapitalist (Post 5901167)
Which distro do you have, including version?

Code:

lsb_release -rd

Code:

#lsb_release -rd
Description:        Manjaro Linux
Release:        17.1.12


Turbocapitalist 09-08-2018 06:57 AM

Ok. That is an Arch derivative so you might need the package "perl-rename". I recommend that.

Or with the lame variant you might try the following but I can't test them:

Code:

rename -n 's/^([^-]+) - ([^-]+).mp3/\2 - \1.mp3$/' *.mp3

# or

rename -n 's/^([^-]*) - ([^-]*).mp3/\2 - \1.mp3$/' *.mp3


BW-userx 09-08-2018 07:03 AM

you can use the mp3 tags to get the info off of them then rename the file.
Code:

#!/bin/bash

working_dir=

count="$(find "$working_dir"  -type f -iname "*.mp3" | wc -l)"

while read f ; do
   
    echo
    echo "$((count--)) :Left:"
    echo
 
#get tag info

    artist="$(exiftool -p '$Artist' "$f")"
 #  album="$(exiftool -p '$Album' "$f")"
    title="$(exiftool -p '$Title' "$f")"
 #  genre="$(exiftool -p '$Genre' "$f")"
 
  newName="$artist"-"$title".mp3

  mv "$f" "${f%/*}"/"$newName"
   
done <<<"$(find "$working_dir" -type f -iname "*.mp3" )"

taken from one of my scripts, and modified a bit. as with every script, do a test run prior to putting it into production. requires exiftool, or perl-exiftool, or by whatever name the Disto has for it in their repo. base name is: exiftool (by perl).

manjaro is, perl-image-exiftool I just looked. ;)

zetrotrack000 09-08-2018 07:21 AM

Quote:

Originally Posted by Turbocapitalist (Post 5901171)
Ok. That is an Arch derivative so you might need the package "perl-rename". I recommend that.

Or with the lame variant you might try the following but I can't test them:

Code:

rename -n 's/^([^-]+) - ([^-]+).mp3/\2 - \1.mp3$/' *.mp3

# or

rename -n 's/^([^-]*) - ([^-]*).mp3/\2 - \1.mp3$/' *.mp3


Following command worked:
Code:

perl-rename 's/^([^-]+) - ([^-]+).mp3/\2 - \1.mp3/' *.mp3
Thanks a lot.


All times are GMT -5. The time now is 10:16 PM.