[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how do I write a shell script to batch rename files in a directory?
From: |
Chris F.A. Johnson |
Subject: |
Re: how do I write a shell script to batch rename files in a directory? |
Date: |
Fri, 29 Aug 2008 21:14:41 +0000 |
User-agent: |
slrn/0.9.8.1 (Linux) |
On 2008-08-29, Bob Proulx wrote:
> Tony Zanella wrote:
>> I have a directory listing of files like:
>> img.bc03.547.1.gif?
>> I need to trim the last character off for each file in the dir.
>> I know I can use:
>> mv img.bc03.547.1.gif? img.bc03.547.1.gif
>> to rename each by hand, but I want to do this as a batch.
>> I know it would start with:
>> for files in *; do;
>> after that, I'm stuck!
>> Any suggestions?
>
> The easiest way is to use the 'rename' command which is usually
> installed as part of Perl.
>
> rename --verbose 's/.gif\?$/.gif/' *.gif?
There are at least two different versions of rename, and their
syntax differs.
> But bash can of course be used to write a small command line program
> to do this too. Here is one way (there are many):
>
> for i in *.gif?; do mv --verbose $i ${i%\?}; done
That will break if there are pathological filenames.
for i in *.gif?; do
mv -v -- "$i" "${i%\?}"
done
--
Chris F.A. Johnson, webmaster <http://Woodbine-Gerrard.com>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)