I have in directory /media/AUDIO/WAVE many .mp3 files with names like:
my filename_01of02.mp3
my filename_02of02.mp3
Your File_01of06.mp3
Your File_02of06.mp3
etc....
In the same directory, /media/AUDIO/WAVE, I have many folders with names like
9780743579490
9780743579491
etc..
Inside the folders I have files:
(Inside folder 9780743579490 is)
9780743579490_01of6_Your File.wav
9780743579490_02of6_Your File.wav
What I need, is a script to find what folder the .wav files that have the same filename at the .mp3 files in /media/AUDIO/WAVE, and move the mp3 files into the folders with the wav files that share the same file name.
To be clear the mp3 part "01of06" is in a different place than the .wav file and the wav file has a 13 digit number in the beginning that the mp3 doesnt, so you will have to match what happens before the first underscore on the mp3 file with what happens between the last underscore and the file extension in the .wav file!
Your example dosn't share the same file name: even excluding the directory_name + _ prefix we still have Your File_01of06 and 01of6_Your File .
Do all the files names have this NNofMM part moved in this way?
Script (Maybe somebody could adapt it to handle filenames with spaces too)
mp3=$(find . -name "*.mp3" | awk -F"/" '/[0-9]/{print}')
wav=$(find . -name "*.wav" | awk -F"/" '/[0-9]/{sub(/\.wav/,"");print $NF}')
for i in $mp3
do
for j in $wav
do
temp_m=$(basename $i)
temp_w=$(basename $j)
if [ ${temp_m%.*} == ${temp_w#*_} ]; then
cp ${temp_m} ~/media/AUDIO/WAVE/${temp_w%%_*} /${temp_m} # Copy file found to test, when you test it, change to "mv"
else
echo "There is no $temp_m in any subfolder or has been moved before"
fi
done
done
Here is one that works with spaces in file name, but we still have issue with NNofMM in wrong place (see my previous question).
WAVDIR="~/media/AUDIO/WAVE/"
find . -name "*.mp3" | while read file
do
temp_m=$(basename "$file" .mp3)
look=$(echo "$WAVDIR/*/*${temp_m}.wav" | sed 's/ /\\ /g')
look=$(eval echo $look)
if [ -f "$look" ]
then
todir="${look%/*}"
echo mv "${temp_m}.mp3" ${todir}/ # echo action to perform - after testing remove echo
else
echo "No unique match for $file ($look)"
fi
done
If the position and format of NNofMM is changed on all files as shown in the example, replace line 5 above with:
Thanks for show other way to do it and solve spaces issue!
Yes, It works with spaces in filename, but when I replace 5 line with code you suggest I didn't receive any file moved.
I'm not sure what if the scenario to change in file names, I did it like glev2005 said in firts post:
Under /media/AUDIO/WAVE/ a file named "my filename_02of02.mp3" and under /media/AUDIO/WAVE/9780743579491/
a file named "9780743579491_02of02_my filename.wav".