From owner-freebsd-questions@FreeBSD.ORG Tue Aug 17 15:34:48 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 410EE1065674 for ; Tue, 17 Aug 2010 15:34:48 +0000 (UTC) (envelope-from drew@mykitchentable.net) Received: from smtp2.mc.surewest.net (qsmtp.mc.surewest.net [66.60.130.145]) by mx1.freebsd.org (Postfix) with SMTP id 134448FC12 for ; Tue, 17 Aug 2010 15:34:47 +0000 (UTC) Received: (qmail 7656 invoked from network); 17 Aug 2010 08:41:10 -0700 Received: by simscan 1.1.0 ppid: 7652, pid: 7653, t: 0.1833s scanners: regex: 1.1.0 attach: 1.1.0 spam: 3.1.7-deb X-Spam-Checker-Version: SpamAssassin 3.1.7-deb (2006-10-05) on smtp2.surewest.net. X-Spam-Level: * X-Spam-Status: No, score=1.8 required=13.5 tests=RATWARE_GECKO_BUILD autolearn=disabled version=3.1.7-deb X-Spam-CMAE-Analysis: v=1.0 c=1 a=swQFnGDQCVwA:10 a=VphdPIyG4kEA:10 a=8nJEP1OIZ-IA:10 a=jDt-9pEAAAAA:8 a=mPBPPn2Axsxpp-04sT4A:9 a=2n84jIoI8VDx78_7bqIA:7 a=WY0oYddADGegqfJIUBxSY8UfB7MA:4 a=wPNLvfGTeEIA:10 Received: from unknown (HELO blacklamb.mykitchentable.net) (69.62.230.77) by smtp2 with SMTP; 17 Aug 2010 08:41:09 -0700 Received: from [127.0.0.1] (tagalong.mykitchentable.net [192.168.2.3]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by blacklamb.mykitchentable.net (Postfix) with ESMTPSA id EAD3F164959 for ; Tue, 17 Aug 2010 08:34:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mykitchentable.net; s=default; t=1282059286; bh=D55DBj1lQBesPAhptg4jzCC0HXdRMgKtBB7eNGnYw4c=; h=Message-ID:Date:From:MIME-Version:To:Subject:References: In-Reply-To:Content-Type:Content-Transfer-Encoding; b=psmUfbAapzp4mCobdWW6oN88bQ4gh7EfbH7oLBjJLT8SQErpNgvyi0pxuTX09UiBI nuYTwVVmapqKGqserg7E0G8QSSoOq9/URcfBLjbwarHdDdoef6ocWsFMQ9L6ofcBpn yOh5Md7RVCaXHAexogEa6RJbIFIqBadsjmO2NUys= Message-ID: <4C6AAC3D.30400@mykitchentable.net> Date: Tue, 17 Aug 2010 08:35:25 -0700 From: Drew Tomlinson User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.11) Gecko/20100711 Thunderbird/3.0.6 MIME-Version: 1.0 To: FreeBSD Questions References: <4C6AA0FD.8000100@mykitchentable.net> <20100817152217.GF3974@libertas.local.camdensoftware.com> In-Reply-To: <20100817152217.GF3974@libertas.local.camdensoftware.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 100817-0, 08/16/2010), Outbound message X-Antivirus-Status: Clean Subject: Re: Bash Script Help - File Names With Spaces X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Aug 2010 15:34:48 -0000 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