0

I have searched and a lot of topics dance around what I am trying to accomplish. I have over 2,000 m4a files buried in with 17,000 mp3s. The directory structure is /home/me/Music/MP3/Artist/Album/song.m4a. I want to use 'find' to discover the m4a songs, move them, their album directory, and their artist directory to /home/me/Music/M4A/Artist/Album/song.m4a. I have been unsuccessful with the mv -exec switch and I have been unsuccessful using basename and/or dirname to create a script. The parent and grand-parent directories have me thrown. Moving the files themselves are not a problem, just creating the directory structure AND moving the files. In a piecemeal effort, I have exported the file list find /home/me/Music/MP3 -name "*.m4a" >> dir.sh (partly because I wanted to see the file locations and # of songs). I then ran sed 's/MP3/M4A/g' dir.sh to replace the MP3 with M4A. Dropping the song.m4a as in this sample from the dir.sh will leave me with a list of Artist/Album directories to run through a while loop with mkdir: /home/me/Music/M4A/Metallica/Re-load/Metallica - The Unforgiven.m4a. Unfortunately, this is where I am stuck, dirname yields a '.'

4
  • Take a look here to format your question: stackoverflow.com/editing-help Commented Mar 28, 2016 at 23:21
  • 1
    it will be more in line with the StackOverflow philosophy if you include your best attempt to solve your problem. In general, use a small test test, and just use echo mv .... until your get your Dir Names correct. OR maybe cd .../MP3; find . -name '*.m4a' | xargs mv {} ../M4A (after making that M4A dir ahead of time). Good luck. Commented Mar 28, 2016 at 23:25
  • Thank you. I wasn't sure it was appropriate to post my failed script. I have tried so many different approaches it would have taken up a lot of space here. I am glad for the direction. Commented Mar 29, 2016 at 2:19
  • @Ergo : Always appropriate (and polite (and expected);-) ) that you post your code. Verbal descriptions are often ambiguous and subject to misunderstandings and conflicting interpretations. Including small sample data, required output, current code, output and error msgs eliminate most issues about describing your problem. It's evidence based! Good luck. Commented Mar 30, 2016 at 3:11

1 Answer 1

1
find /home/me/Music/MP3 -name \*.m4a| sed -e 's/.*/mkdir -p $(dirname "&"); mv "&" "&"M4A;/' | sed -e 's/MP3\([^"]*\)"M4A$/M4A\1"/' > moveit_baby.sh
bash moveit_baby.sh

should do the job, but check "moveit_baby.sh" before you call it.

Depending on your sed implementation you will need \(\) or plain () in the second sed. Of course the string "MP3" should neither be part of Artist, Album or song name, otherwise you need a more complex filter, see below.

You might further optimize when you insert mkdir -p only if the dirname changes. Such more complex decisions on input parameters are better achieved with while read loops

find /home/me/Music/MP3 -name \*.m4a | while read file
do
    # do anything you want to $file here
done
Sign up to request clarification or add additional context in comments.

2 Comments

After some research I believe sed is the way to go, but the code above has the same problem that my mv script has....the parent and grandparent directories are not getting moved/created. The code above has the essence of renaming the files with an additional 'MP4' at the end of the song and never moving anywhere. Because I am missing the key element of working with the directories, a loop, although a good idea, can't be used just yet. I'll research 'sed' a little more and post if I come up with a solution.
assuming part of your problem is due to spaces in file and dir names. Be sure to dbl-quote all usage of any "$dir" and "$file" vars. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.