Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
03-10-2009, 06:28 PM
|
#1
|
LQ Guru
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that.
Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700
Rep:
|
Need a little scripting help on moving files.
Over all not bad on linux and can do some basic scripting but just not ever fully understood stuff like awk and sed and others. Here is what i have done. I ripped my entire cd collection using Xripper and in the process saved the wav file as well converted to mp3 at the time. Plus used oggenc to convert wav to ogg. Now the structure of it is the artist name > CD title > and a wav, mp3, and ogg of each song on the cd. So now i have about 120 gig of music. What I want to do is create three directories WAV, MP3, and OGG and create the same structure in each like artist > cd title > and the wav in the wav folder and so fore on the others. Any help would be greatly appreciatied.
Currently as this
Music > Artist > CD Title > WAV, MP3, OGG
Want it to be.
Music > WMV > Artist > CD Title > WAV
Music > MP3 > Artist > CD Title > MP3
Music > OGG > Artist > CD Title > OGG
Brian
|
|
|
03-11-2009, 11:06 AM
|
#2
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
It probably wouldn't be that difficult to create such a script, but it might be a bit tricky to get the correct text strings into the proper variables necessary for creating the new file tree. Any spaces or other reserved characters in the filenames might be especially problematic, for example.
I think we'd need a bit more detail on exactly what the directory structure looks like and how you want the output to come out. An actual example of the file structure and named files would help.
|
|
|
03-12-2009, 06:20 AM
|
#3
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
After reading your post again, I realize that your request isn't as complicated as I thought it was yesterday. I've made a few assumptions and worked up a starter script for you. Note that it doesn't do any error checking, so don't run it on anything you want to keep until you've thoroughly tested it.
Code:
#!/bin/bash
#run this script in the base "Music" directory.
IFS="
" #changes the field separator to newline to make
#dealing with spaces in filenames easier.
for DIR in $(find . -maxdepth 2 -mindepth 2 -type d -print); do
# uses find to list all subdirectories
# with exactly two nested layers (artist/album)
DIR="${DIR#./}" #strip unwanted "./" from directory name.
echo "processing ${DIR}"
mkdir -p "ogg/${DIR}" #create the target directories
mkdir -p "wav/${DIR}" #using the original directory name
mkdir -p "mp3/${DIR}"
mv "${DIR}"/*.ogg ogg/"${DIR}" #Move each file type to the
mv "${DIR}"/*.mp3 mp3/"${DIR}" #directories created above.
mv "${DIR}"/*.wav wav/"${DIR}"
DELDIR+="${DIR%/*}
" #add artist part of dir to variable for later
#deletion. Note the newline break.
done
for line in $DELDIR; do
echo rm -rf $line #remove the echo to have it
#delete the original directories.
done
exit 0
Now for some explanations.
I'm assuming that all the directories you want to run it on are in Music/Artist/Album, and that there are no other files or directories to deal with. When you run the script in Music, it will use find to hunt down all ./DirA/DirB nested directories. It will then create Music/ogg/DirA/DirB, Music/mp3/DirA/DirB, and Music/wav/DirA/DirB directories and move the appropriate files into them.
By the way, does anyone know how to make find output file paths without the leading "./" in them? It's rather annoying to have to strip them off again later.
Edit: D'oh! I originally had a line to remove the original base directories, but I realized that I couldn't get it to remove everything without possibly destroying unprocessed directories as well, so I stripped it. I'll add it back when I can figure out how to do it right.
Edit2: Ok, got it working. Now cats the artist (not album) part of each DIR name to a variable, which is then used to batch-delete everything at the end.
Last edited by David the H.; 03-13-2009 at 05:43 AM.
Reason: fix the script
|
|
|
03-12-2009, 05:23 PM
|
#4
|
LQ Guru
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that.
Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700
Original Poster
Rep:
|
Thanks for your help. I have not had a chance to experiment with it but looks simplier than I was thinking. I sometimes go overboard in my thinking. I perfer the keep it simply method as much as possible. Will give it a try this weekend after making a backup of it and experiment on the backup.
Again thank you for your time and help.
Brian
|
|
|
03-13-2009, 05:46 AM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Let me know how it works out.
And I notice I made another mistake in my last post. I meant to say that the last line adds the artist name to the DELDIR variable, not the album. If you "rm -rf" the artist directory, it will of course also automatically remove the album directories it contains as well.
|
|
|
03-16-2009, 09:19 AM
|
#6
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
3 suggestions
I suggest strongly considering replacing ALL the spaces in the file & directory names w/ underscores. Sample code:
Code:
ls *\ * -1 | while read N
do mv "$N" `echo "$N" |tr \ _`
done
Run this recursively 3 times: - On Music to "fix" the Artist names.
- On Music/Artist to "fix" the CD Titles.
- On Music/Artist/CD_Title to "fix" the track file names.
I think this might be as easy as changing to & then to but I didn't fully test it. (I did test the sample code.)
Instead of:
Code:
mkdir -p "ogg/${DIR}" #create the target directories
mkdir -p "wav/${DIR}" #using the original directory name
mkdir -p "mp3/${DIR}"
try (tested):
Code:
mkdir -p {ogg,wav,mp3}/"${DIR}" #create the target directories
Less typing == fewer chances for typos.
Finally, instead of doing mo ves & deleting the old structure, why not just put links in the new directories? Hard or soft, your choice. You would then have the old structure if you needed it, & wouldn't double the storage space. You also wouldn't have a built-in back up.
|
|
|
All times are GMT -5. The time now is 07:25 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|