From: Jeff Woods Subject: Re: renaming multiple files Date: Thu, 09 Dec 2004 14:02:18 -0700 Message-ID: <6.1.2.0.0.20041209135423.0341cb68@mail.speakeasy.net> References: <41A20C74.4050709@fcaglp.unlp.edu.ar> <16802.37568.605213.224819@cerise.gclements.plus.com> <6.1.2.0.0.20041207175728.02fbe190@mail.spamcop.net> <16824.41702.114498.362890@cerise.gclements.plus.com> Mime-Version: 1.0 Return-path: In-Reply-To: <16824.41702.114498.362890@cerise.gclements.plus.com> References: <41A20C74.4050709@fcaglp.unlp.edu.ar> <16802.37568.605213.224819@cerise.gclements.plus.com> <6.1.2.0.0.20041207175728.02fbe190@mail.spamcop.net> <16824.41702.114498.362890@cerise.gclements.plus.com> Sender: linux-admin-owner@vger.kernel.org List-Id: Content-Type: text/plain; charset="us-ascii"; format="flowed" Content-Transfer-Encoding: 7bit To: Glynn Clements Cc: zyserman@fcaglp.unlp.edu.ar, linux-admin@vger.kernel.org At 12/9/2004 07:09 PM +0000, Glynn Clements wrote: >Jeff Woods wrote: >>> for file in prefix-* ; do >>> mv "$file" "${file##prefix-}" >>> done >>What happens if one of the files is named "prefix-prefix-1"? > >It gets renamed to prefix-1, which is what I would expect. No. Because you used two of "##" it removes all instances of "prefix1" from the front of the variable and the resulting filename would be "1". >>What about "prefix-"? >> >>I suggest: >> >>for file in prefix-?* >>do >>mv "$file" "${file#prefix-}" >>done > >Huh? I added "?" to the pattern so that a file with the exact name "prefix-" would not be selected since that would result in a modified filename that was an empty string. I also reduced the "##" to "#" so it only removes one "prefix-" from the beginning even if there are more than one. Like I said, "Almost correct." Your version works in all cases except for filenames matching patterns "prefix-" and "prefix-prefix-*" where it would fail or give what are probably undesired results. -- Jeff Woods