LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-04-2012, 03:11 PM   #1
BoydRice
Member
 
Registered: Jul 2008
Location: Tacoma, WA
Distribution: Slackware64
Posts: 96

Rep: Reputation: 19
create sub-folder from folder name and transfer files on the fly


I have a bunch of music that is currently stored in folders named like Artist--Album Name then inside are the tracks from that album. I would like to know if there is a way to sweep through my music directory and parse the Album Name from the folder name, create a sub-folder with the Album Name and move the tracks into it. So instead of Artist--Album Name then tracks it would be /music/Artist/Album Name/tracks. I am sure there has to be a way to accomplish this but I am a little lost on how would be the best way to achieve it. If anyone has any tips or suggestions I would love to hear them. Thanks in advance for any help.
 
Old 03-04-2012, 04:30 PM   #2
Ocean_82
LQ Newbie
 
Registered: Mar 2012
Location: Uk
Distribution: Linux Mint 12
Posts: 2

Rep: Reputation: Disabled
A python script using the os module and shutil module to move the files would be an easy approach. The os module can make directories and shutil to move files.

For directory in os.path(pathtomusic):
folder = pathtomusic +'/' + directory
If os.path.isdir(folder):
artist, album = directory.strip().split('-')
artistfolder = "pathtomusic" + '/' + artist
os.mkdir(pathtomusic+'/'+artist)
os.mkdir(artistfolder '/' + album)

The for each file in the folder do a similar split and compare if album = filename album shutil.move it to new folder... Something like this anyways...

Sorry for breif and possibly inaccurate reply but I'm typing this out on my phone ...

Last edited by Ocean_82; 03-04-2012 at 04:52 PM. Reason: Missed something
 
Old 03-05-2012, 12:13 AM   #3
BoydRice
Member
 
Registered: Jul 2008
Location: Tacoma, WA
Distribution: Slackware64
Posts: 96

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by Ocean_82 View Post
A python script using the os module and shutil module to move the files would be an easy approach. The os module can make directories and shutil to move files.

For directory in os.path(pathtomusic):
folder = pathtomusic +'/' + directory
If os.path.isdir(folder):
artist, album = directory.strip().split('-')
artistfolder = "pathtomusic" + '/' + artist
os.mkdir(pathtomusic+'/'+artist)
os.mkdir(artistfolder '/' + album)

The for each file in the folder do a similar split and compare if album = filename album shutil.move it to new folder... Something like this anyways...

Sorry for breif and possibly inaccurate reply but I'm typing this out on my phone ...
Thanks, I will give that a try. I've never messed with python before so I will see how it goes.
 
Old 03-05-2012, 02:37 PM   #4
Ocean_82
LQ Newbie
 
Registered: Mar 2012
Location: Uk
Distribution: Linux Mint 12
Posts: 2

Rep: Reputation: Disabled
Better Script

Hey, Since you don't do much python... Thought I'd help point you in the right direction.

There are probably loads of better ways to do this, but this will work.. but test it first!! and back up the music files first


Code:
import os
import shutil
from os.path import isfile, join

musicfolder = "/home/YourHome/Music"

if __name__ == "__main__":
	
	#iterate over directories in music folder
	for directory in os.listdir(musicfolder):
		if os.path.isdir(musicfolder + '/' + directory):
			folder = musicfolder + '/' + directory
			#split directory name based on '--' separator
			artist, album = directory.split('--')
			
			#make artist folder
                        artistfolder = musicfolder + '/' + artist
			if not os.path.exists(musicfolder + '/' + artist):
				os.mkdir(artistfolder)

			#make album folder	
                        albumfolder = artistfolder + '/' + album
			if not os.path.exists(artistfolder + '/' + album):
				os.mkdir(albumfolder)
				
			#move each file	
			for song in os.listdir(folder):
				if isfile(join(folder,song)):
					filepath = join(folder, song)
					shutil.move(filepath, albumfolder)
					print "moved: " + filepath + " to " + albumfolder
			
			#delete empty directory
			if not os.listdir(folder):
				os.rmdir(folder)

Last edited by Ocean_82; 03-05-2012 at 02:40 PM.
 
Old 03-05-2012, 04:15 PM   #5
BoydRice
Member
 
Registered: Jul 2008
Location: Tacoma, WA
Distribution: Slackware64
Posts: 96

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by Ocean_82 View Post
Hey, Since you don't do much python... Thought I'd help point you in the right direction.

There are probably loads of better ways to do this, but this will work.. but test it first!! and back up the music files first


Code:
import os
import shutil
from os.path import isfile, join

musicfolder = "/home/YourHome/Music"

if __name__ == "__main__":
	
	#iterate over directories in music folder
	for directory in os.listdir(musicfolder):
		if os.path.isdir(musicfolder + '/' + directory):
			folder = musicfolder + '/' + directory
			#split directory name based on '--' separator
			artist, album = directory.split('--')
			
			#make artist folder
                        artistfolder = musicfolder + '/' + artist
			if not os.path.exists(musicfolder + '/' + artist):
				os.mkdir(artistfolder)

			#make album folder	
                        albumfolder = artistfolder + '/' + album
			if not os.path.exists(artistfolder + '/' + album):
				os.mkdir(albumfolder)
				
			#move each file	
			for song in os.listdir(folder):
				if isfile(join(folder,song)):
					filepath = join(folder, song)
					shutil.move(filepath, albumfolder)
					print "moved: " + filepath + " to " + albumfolder
			
			#delete empty directory
			if not os.listdir(folder):
				os.rmdir(folder)
Awesome! I will give this a try tonight, thank you so much for the help!
 
Old 03-07-2012, 07:44 PM   #6
BoydRice
Member
 
Registered: Jul 2008
Location: Tacoma, WA
Distribution: Slackware64
Posts: 96

Original Poster
Rep: Reputation: 19
Ocean 82, thanks so much for this it totally did the trick! Thanks!
 
  


Reply

Tags
bash, command line, folder, music



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using terminal command -Find files in a folder and copy them to a different folder j-jock Linux - General 4 11-28-2011 02:20 AM
[SOLVED] How to list folder size, and number of files and folders in folder steven.c.banks Linux - General 3 11-24-2010 06:24 AM
create a folder/file within a folder using perl indu.a Programming 2 03-07-2007 10:09 PM
cannot create files/folder in Linux lux Linux - General 4 12-14-2005 02:21 AM
how can i default the max folder file size when it create inside a folder antony_csf Linux - Software 1 06-17-2004 02:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:28 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