Bash Script Help - File Names With Spaces
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? Thanks, Drew -- Like card tricks? Visit The Alchemist's Warehouse to learn card magic secrets for free! http://alchemistswarehouse.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: Bash Script Help - File Names With Spaces
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@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org -- Sterling (Chip) Camden| sterl...@camdensoftware.com | 2048D/3A978E4F http://camdensoftware.com | http://chipstips.com| http://chipsquips.com pgpCHrUZ30LlM.pgp Description: PGP signature
Re: Bash Script Help - File Names With Spaces -- SOLVED
On 8/17/2010 7:47 AM, Drew Tomlinson wrote: 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? It finally occurred to me that I needed the shell to see a new line as the delimiter and not whitespace. Then a simple search revealed my answer: O=$IFS IFS=$(echo -en \n\b) do stuff IFS=$O Sorry for the noise. Drew -- Like card tricks? Visit The Alchemist's Warehouse to learn card magic secrets for free! http://alchemistswarehouse.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: Bash Script Help - File Names With Spaces
On 8/17/2010 8:22 AM, Chip Camden wrote: 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 for your reply. I like this better than manipulating $IFS because then I don't have to set it back. Cheers, Drew -- Like card tricks? Visit The Alchemist's Warehouse to learn card magic secrets for free! http://alchemistswarehouse.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: Bash Script Help - File Names With Spaces
Am Dienstag, den 17.08.2010, 08:22 -0700 schrieb Chip Camden: find -E ... | while read i; do; basename $i; done The semicolon behind do isn't necessary. -- Timm ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: Bash Script Help - File Names With Spaces
On Tue, 17 Aug 2010 07:47:25 -0700, Drew Tomlinson d...@mykitchentable.net said: D Then I attempt to use 'basename' to extract the file name to a variable D which I can later pass to 'ln'. This seems to work: D basename /archive/Multimedia/Audio/Music/Billboard Top USA D Singles/1980-028 Kenny Loggins - This Is It.mp3 This is a subset of a larger problem: getting the last field from a set of delimited records which may not all have the same number of fields. I've used this when I needed basenames for ~500,000 files: find . regex-or-print-or-whatever | rev | cut -f1 -d/ | rev For dirnames: find . regex-or-print-or-whatever | rev | cut -f2- -d/ | rev | sort -u -- Karl Vogel I don't speak for the USAF or my company When I'm feeling down, I like to whistle. It makes the neighbor's dog run to the end of his chain and gag himself.--unknown ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: Bash Script Help - File Names With Spaces -- SOLVED
Drew Tomlinson d...@mykitchentable.net writes: It finally occurred to me that I needed the shell to see a new line as the delimiter and not whitespace. Then a simple search revealed my answer: O=$IFS IFS=$(echo -en \n\b) do stuff IFS=$O Old IFS value can be preserved by using `local' keyword or (...) braces, too. It's a bit better than polluting global scope with temporary variable. $ echo -n $IFS | (vis -w; echo) \040\^I\^J $ for i in $(find . -type f); do echo $i; done ./My Long File Name ./Another File $ f() { local IFS=; eval $@; } $ f 'for i in $(find . -type f); do echo $i; done' ./My Long File Name ./Another File $ (IFS=; for i in $(find . -type f); do echo $i; done) ./My Long File Name ./Another File $ echo -n $IFS | (vis -w; echo) \040\^I\^J ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org