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

Voila.

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 archive.org links and copied content of the email from the first link.

Leave a Reply

%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close