linuxbashfilecp

How we can find & coping .mp3 files recursively in linux into a single folder


Basically I am writing this to transfer all .mp3 files to my mobile through Bluetooth option. If I am directly coping it to SD Card, it is not working. My button phone not recognizing the MP3 files which I've copied it from my computer.

IF, somehow I can copy the MP3 files into a single folder, I can easily transfer all my files into my phone by selecting all files with + option in Bluetooth file transfer.

I had a backup script written once by myself that you can find that it here.

#!/usr/bin/bash

#------------backupscript--------------

#mkdir ~/backup/`date +%d-%m-%Y`
#for i in `ls ~/repos`
#do
#cp -r ~/repos/`echo $i` ~/backup/`date +%d-%m-%Y`/`echo $i` #recursively copies the folder
#done


#------------backupscript--------------

This I have written to take a backup of my svn repos

To list the .mp3 files I found the following command from internet, like,

for i in `find . -name *.mp3 -exec echo {} ;`

from the link, - How can I do foreach *.mp3 file recursively in a bash script?

Based on that I've altered my shell backup script which I've written like,

for i in `find . -name *.mp3 -exec echo {} \;`
do
cp `echo $i` ./oneList/
done

it works for me coping my all mp3 files into, but I am getting errors like I have put in the below stanza,

cp: cannot stat 'Jeevan': No such file or directory cp: cannot stat '(sad).mp3': No such file or directory cp: cannot stat './585/Geethanjali/Oru': No such file or directory cp: cannot stat 'Jeevan.mp3': No such file or directory cp: cannot stat './585/Geethanjali/Otha': No such file or directory cp: cannot stat 'Ruba.mp3': No such file or directory

Later I found discrepancies in the no. of files.

enter image description here

enter image description here

What I am doing wrong over here coping mp3 files into single folder using the bash script which I am writing.

What I have to alter in my script.


Solution

  • If you've got a non-ancient version of Bash you don't need to use find. Try this Shellcheck-clean code:

    #! /bin/bash -p
    
    shopt -s dotglob globstar nullglob
    for mp3file in **/*.mp3; do
        cp -v -- "$mp3file" oneList/
    done