Bash Script Help - File Names With Spaces

Chip Camden sterling at camdensoftware.com
Tue Aug 17 15:22:24 UTC 2010


Quoth Drew Tomlinson on Tuesday, 17 August 2010:
> I have a collection of yearly top 100 Billboard mp3s in this format (all 
> one line - sorry if it wraps):
> 
> /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny 
> Loggins - This Is It.mp3
> 
> I want to create symbolic links to the top 30 in 1966-1969 in another 
> directory for easy migration to a flash card. Thus I invoked 'find' to 
> get a list (again, all one line):
> 
> find -E "/archive/Multimedia/Audio/Music/Billboard Top USA Singles" 
> -regex '.*19[6-9][0-9]-0[0-2][0-9].*'
> 
> (OK, I know this will only return the top 29)
> 
> 'find' returns the complete filename as above:
> 
> /archive/Multimedia/Audio/Music/Billboard Top USA Singles/1980-028 Kenny 
> Loggins - This Is It.mp3
> 
> Then I attempt to use 'basename' to extract the file name to a variable 
> which I can later pass to 'ln'.  This seems to work:
> 
> basename "/archive/Multimedia/Audio/Music/Billboard Top USA 
> Singles/1980-028 Kenny Loggins - This Is It.mp3"
> 
> returns (all one line):
> 
> 1980-028 Kenny Loggins - This Is It.mp3
> 
> which is what I would expect.  However using it with 'find' give me this 
> type of unexpected result:
> 
> for i in `find -E "/archive/Multimedia/Audio/Music/Billboard Top USA 
> Singles" -regex '.*19[6-9][0-9]-0[1-2][0-9].*'`; do basename "${i}";done
> 
> 1980-028
> Kenny
> Loggins
> -
> This
> Is
> It.mp3
> 
> Why is this different? And more importantly, how can I capture the file 
> name to $i?

Try:

find -E ... | while read i; do; basename $i; done

When using back-ticks, all the output gets appended together,
space-separated.  Then 'for' can't tell the difference between a space in
a filename and a delimiter.  Using 'read' instead preserves line
boundaries.

> 
> Thanks,
> 
> Drew
> 
> -- 
> Like card tricks?
> 
> Visit The Alchemist's Warehouse to
> learn card magic secrets for free!
> 
> http://alchemistswarehouse.com
> 
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"

-- 
Sterling (Chip) Camden    | sterling at camdensoftware.com | 2048D/3A978E4F
http://camdensoftware.com | http://chipstips.com        | http://chipsquips.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20100817/380ee839/attachment.pgp


More information about the freebsd-questions mailing list