views:

291

answers:

3

I'm trying to recover a mates hard drive, there is no structure what so ever so music and images are everywhere but in named folders sometimes >5 folders deep, I've managed to write a one-liner that finds the files and copies them to a mounted drive but it preserves the file structure completely. What I'm after is a bit of code that searches the drive and copies to another location and copies just the parent folder with the mp3/jpg files within and not the complete path. The other issue I have is the music is /folder/folder/folder/Artist/1.mp3..2.mp3..10.mp3 etc etc so I have to preserve the folder 'Artist' to give him any hope of finding his tracks again.

What I have working currently:

find /media/HP/ -name *.mp3 -fprintf /media/HP/MUSIC/Script.sh 'mkdir -p "/media/HP/MUSIC/%h" \n cp "%h/%f" "/media/HP/MUSIC/%h/"\n'

I then run the script.sh and it does all the copying.

Many Thanks

+1  A: 

What you probably want to do will be along the lines of:

mkdir "$dest/$(basename $(dirname $source))"
Dennis Williamson
A: 

If you are doing the operation recursively ( entering directory by directory ), what you can do is everytime save your path as: Road_Dir=$(pwd)(let's say dir1/dir2/dir3/)

Then you detect your artist_name directory you save it is Music_Dir=$(pwd) Finally you could extract your artist_name directory with the simple command:

Final_Dir=${Music_Dir##$Road_Dir/} (wich means take out the string $Road_Dir/ from $Music_Dir beginning from the left of $Music_Dir)

With this Final_Dir will contain artist_name, and you can copy your music file as $Final_Dir/Music.mp3 ...

+1  A: 

OK folks - thanks for the input it did make me think deeper about this and I've come up with a result with the help of a colleague (thanks SiG):

This one-liner finds the files, and writes a script file to run separately but does copy across just the last folder as I wanted initially.

The Code:

find /some/folder/ -name *.mp3 |  awk 'BEGIN{FS="/"}{print "mkdir -p \"/some/new/place/" $(NF-1) "\"\ncp -v -p \"" $0 "\" \"/some/new/place/" $(NF-1) "/" $NF "\""}' > script.sh

The output is:

mkdir -p "/media/HP/MUSIC/Satize"  cp -v -p "/media/HP/Users/REBEKAH/Music/Satize/You Don't Love Me.mp3" "/media/HP/MUSIC/Satize/You Don't Love Me.mp3"

When script.sh is run it does all the work and I end up with a very reduced file structure I can copy to a new drive.

Thanks again folks much appreciated. KjF

Kevin F