LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 03-10-2009, 05:28 PM   #1
Brian1
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: Reputation: 65
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
 
Old 03-11-2009, 10:06 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 03-12-2009, 05:20 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 04:43 AM. Reason: fix the script
 
Old 03-12-2009, 04:23 PM   #4
Brian1
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: Reputation: 65
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
 
Old 03-13-2009, 04:46 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 03-16-2009, 08:19 AM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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:
  1. On Music to "fix" the Artist names.
  2. On Music/Artist to "fix" the CD Titles.
  3. On Music/Artist/CD_Title to "fix" the track file names.
I think this might be as easy as changing
Code:
ls  *\ * -1
to
Code:
ls  */*\ * -R1
& then to
Code:
ls  */*/*\ * -R1
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 moves & 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.
 
  


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
moving files from a location to other, preventing to move incomplete files pogo123 Programming 8 11-14-2008 06:21 AM
moving files from a location to other, preventing to move incomplete files pogo123 Linux - Newbie 2 11-13-2008 01:57 PM
Moving Files except a few pccdrussell Linux - General 7 07-21-2007 11:03 PM
Need help moving files. EYEdROP Linux - Newbie 26 03-07-2007 10:20 PM
moving files that have spaces in variables -bash scripting bhar0761 Programming 10 09-22-2005 07:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:39 PM.

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