This question already has an answer here:

I'm having trouble with this piece of code:

22 for filename in "$( ls "$1" | grep ".*\.flac$" )"; do
23                 file_path="$1${filename}"
24                 ffmpeg -i "${file_path}" -f ffmetadata $HOME/metadata

Instead of a metadata file on each iteration, I'm getting this error message:

Downloads/Ariel Pink's Haunted Graffiti - Worn Copy (2005)/01 Trepanated Earth.flac ... 17 Jagged Carnival Tours.flac: File name too long

So it appears that inside the loop the $filename variable is equal the names of all FLAC files lumped together.

Of course, omitting quote marks on line 22 results in whitespace problems.

How do I make this work? I'm new to bash and very confused.

marked as duplicate by Gilles bash Mar 14 '15 at 12:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • use find instead of your loop? – bsd Mar 14 '15 at 10:31

how about

 for filepath in "$1/"*.flac 
      ffmpeg -i "${file_path}" -f ...


  • "$1/"*.flac will garantee .flac suffix in the end
  • be sure to quote "${file_path}"
  • basename can be found using bn=$(basename "${file_path}")
  • un flac'ed basename can be found using bnnf=$(basename "${file_path}" .flac)


A > ls -l Music 
total 0
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 foo bar
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 foo bar.flac
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 fubar
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 fubar.flac

now run:

 A > for f in Music/*.flac; do echo $f; ls -l "$f" ; done 
Music/foo bar.flac
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 Music/foo bar.flac
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 Music/fubar.flac
  • I haven't used it in my script yet, but I did some debugging and it seems to work. The solution looks so simple I'm confused even further. :) Thank you! – zvukva Mar 14 '15 at 11:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.