Ubuntu – Pass arguments from file line by line to function

bashcommand linescripts

I have list of some mp3's:

song 1.mp3
song 2.mp3
.
.
.
song 349.mp3

I can see their bitrate via right mouse button -> Properties -> Audio/Video,
but also I can check it using the terminal command

file "song 1.mp3"

I'd like to find out which bitrate from my list is most frequently used, so I thought it would be nicely done via shell i.e. shell script.

I would lose too much time if I typed

file "song 1.mp3"; file "song 2.mp3"; ... ; file "song 349.mp3"

So, my question is :

Can we pass arguments line by line from some text file to shell function?
An additional problem is that my song names contain spaces.

Best Answer

Assuming your list of files is one filename per line, the xargs utility should be able to handle filenames with spaces in them if you specify newline as the delimiting character e.g.

xargs -d '\n' file < filelist.txt

If you prefer to use a shell function

while IFS= read -r f; do [[ -z "$f" ]] || file "$f"; done < filelist.txt