Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop over file names from `find`?

If I run this command:

sudo find . -name *.mp3

then I can get a listing of lots of mp3 files.

Now I want to do something with each mp3 file in a loop. For example, I could create a while loop, and inside assign the first file name to the variable file. Then I could do something with that file. Next I could assign the second file name to the variable file and do with that, etc.

How can I realize this using a linux shell command? Any help is appreciated, thanks!

like image 692
Searene Avatar asked Nov 28 '22 09:11

Searene


1 Answers

For this, use the read builtin:

sudo find . -name *.mp3 |
while read filename
do
    echo "$filename"    # ... or any other command using $filename
done

Provided that your filenames don't use the newline (\n) character, this should work fine.

like image 112
Jeremy Kerr Avatar answered Dec 06 '22 09:12

Jeremy Kerr