LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-08-2018, 05:48 AM   #1
zetrotrack000
Member
 
Registered: Dec 2011
Posts: 401

Rep: Reputation: Disabled
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
 
Old 09-08-2018, 05:55 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
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.
 
Old 09-08-2018, 05:58 AM   #3
zetrotrack000
Member
 
Registered: Dec 2011
Posts: 401

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
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?
 
Old 09-08-2018, 06:03 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
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.
 
Old 09-08-2018, 06:04 AM   #5
zetrotrack000
Member
 
Registered: Dec 2011
Posts: 401

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
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.
Attached Thumbnails
Click image for larger version

Name:	rename.png
Views:	33
Size:	14.6 KB
ID:	28554  

Last edited by zetrotrack000; 09-08-2018 at 06:07 AM.
 
Old 09-08-2018, 06:26 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
The mp3 within the brackets is mp3$
 
Old 09-08-2018, 06:35 AM   #7
zetrotrack000
Member
 
Registered: Dec 2011
Posts: 401

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AnanthaP View Post
The mp3 within the brackets is mp3$
Still not working. Please see attached image.
PS: I also tried by putting $ on both mp3
Attached Thumbnails
Click image for larger version

Name:	rename.png
Views:	18
Size:	14.8 KB
ID:	28555  
 
Old 09-08-2018, 06:47 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
Which distro do you have, including version?

Code:
lsb_release -rd
 
Old 09-08-2018, 06:48 AM   #9
zetrotrack000
Member
 
Registered: Dec 2011
Posts: 401

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Which distro do you have, including version?

Code:
lsb_release -rd
Code:
#lsb_release -rd
Description:	Manjaro Linux
Release:	17.1.12
 
Old 09-08-2018, 06:57 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
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
 
1 members found this post helpful.
Old 09-08-2018, 07:03 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 09-08-2018 at 07:15 AM.
 
Old 09-08-2018, 07:21 AM   #12
zetrotrack000
Member
 
Registered: Dec 2011
Posts: 401

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
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.

Last edited by zetrotrack000; 09-08-2018 at 03:05 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Nginx Reverse proxy on a internal apache reverse server ITiger Linux - Software 0 04-25-2014 07:44 AM
[SOLVED] program to reverse a file poonamkhirolia Linux - Newbie 8 12-25-2010 02:33 PM
Reverse DNS lookup from a file mlapl1 Linux - Newbie 3 10-04-2009 08:46 PM
C to Reverse a Text File. aatwell Programming 19 01-24-2009 10:12 AM
Reverse Sort Text File BxBoy Linux - General 1 08-02-2004 10:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration