[SOLVED] create sub-folder from folder name and transfer files on the fly
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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)
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.