How to loop over filenames wit …

How to loop over filenames with spaces – I knew this, but it had disappeared in the recesses of my brain.

> for f in `cat /pathtolist/mp3.txt` do cp "$f" /newdirectory done

You didn't say exactly how the file is organised, so I'm going to
guess one filename per line.  (Note that a filename is allowed
to contain the newline character; I'm just hoping that none of
your files do!)

Others have already pointed out that spaces can be tricky.  I'm
going to show you that there really is no problem.  Rather than
iterate using for, use while and read:

while read filename; do cp "$filename" /newdirectory; done < mp3.txt


Weather’s been bad for the past few days. No real opportunities for photography. Bah! At least I have 99% of my Christmas shopping done. Meanwhile, Mark has an entertaining piece on “The One Toy”.

Oh yes, If you’re over 20, buy RETRO, a (one off?) mag from games mag, Edge. It has great interviews with old Zzap64! staffers, bits on games of old (Elite anyone?) and other great stuff! If you’re under 20, you probably don’t know what I’m talking about. Hmm, scratch that, if you’re under 24 you probably don’t know what I’m talking about! Getting old ..

Edit in 2019: replaced dead links with links and copied content of the email from the first link.

By Donncha

Donncha Ó Caoimh is a software developer at Automattic and WordPress plugin developer. He posts photos at In Photos and can also be found on Twitter.

Leave a Reply