Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

rename doesn't?

6 views
Skip to first unread message

Grant

unread,
Feb 8, 2012, 9:33:15 PM2/8/12
to
Recently somebody suggested 'rename' as a solution to something I
forgot, but after a couple tries 'rename' to remove "_01" from 234
filenames, I resorted to simple CLI stuff:

$ ls > file
$ gawk '/JPG/{inp=$0;sub(/_01/,"");print "mv " inp " " $0}' file>file2
$ head -3 file2
mv DSC_6047_01.JPG DSC_6047.JPG
mv DSC_6048_01.JPG DSC_6048.JPG
mv DSC_6049_01.JPG DSC_6049.JPG
$ sh file2

Did the job, but what did I miss in trying out various 'rename'
options that did no renaming? Combinations of ? and * wildcards
didn't produce what I expected.

Grant.

Lew Pitcher

unread,
Feb 8, 2012, 10:43:24 PM2/8/12
to
On Wednesday 08 February 2012 21:33, in alt.os.linux.slackware,
If I read your script correctly, you started out with filenames like
DSC_6047_01.JPG
DSC_6048_01.JPG
etc, and wanted to transform them into
DSC_6047.JPG
DSC_6048.JPG
by dropping the _01 before the .JPG

rename(1) ('man 1 rename') includes a couple of example usages. The one that
matches your situation would be:

) And
) rename .htm .html *.htm
)
) will fix the extension of your html files.

You could
rename _01.JPG .JPG *_01.JPG


HTH
--
Lew Pitcher

Kees Theunissen

unread,
Feb 8, 2012, 11:02:01 PM2/8/12
to
~/test$ for x in `seq 6047 6055`; do touch DSC_${x}_01.JPG; done
~/test$ ls
DSC_6047_01.JPG DSC_6050_01.JPG DSC_6053_01.JPG
DSC_6048_01.JPG DSC_6051_01.JPG DSC_6054_01.JPG
DSC_6049_01.JPG DSC_6052_01.JPG DSC_6055_01.JPG
~/test$ rename _01. . *
~/test$ ls
DSC_6047.JPG DSC_6049.JPG DSC_6051.JPG DSC_6053.JPG DSC_6055.JPG
DSC_6048.JPG DSC_6050.JPG DSC_6052.JPG DSC_6054.JPG

So replacing "_01." with "." in these names worked.
Let's try if we can use an empty string as replacement string.

~/test$ rename _60 "" *
~/test$ ls
DSC47.JPG DSC49.JPG DSC51.JPG DSC53.JPG DSC55.JPG
DSC48.JPG DSC50.JPG DSC52.JPG DSC54.JPG

That worked too.


Regards,

Kees.

--
Kees Theunissen.

Mikhail Zotov

unread,
Feb 9, 2012, 1:20:13 AM2/9/12
to
In this concrete case, I'd run

rename _01 '' *.JPG

.JPG can be omitted since there are no other files but JPG.

--
M.

Grant

unread,
Feb 9, 2012, 7:31:42 AM2/9/12
to
I did a 'man rename' and didn't see wood for trees ;) Which is why
I posed the problem. Thanks to Kees and Mikhail too for their
illumination with different examples.

>You could
> rename _01.JPG .JPG *_01.JPG

Well yes, I was not giving it three parms, and wondered why nothing
happened ;)

Grant.
>
>
>HTH

Grant

unread,
Feb 9, 2012, 7:36:23 AM2/9/12
to
Yes you're right, had /JPG/ in my filter to avoid reading 'file*' ;)

Grant.

Blumf

unread,
Feb 9, 2012, 11:34:40 AM2/9/12
to
On 02/09/2012 02:33 AM, Grant wrote:
> Recently somebody suggested 'rename' as a solution to something I
> forgot, but after a couple tries 'rename' to remove "_01" from 234
> filenames, I resorted to simple CLI stuff:

Not a CLI solution, but XFCE's file manager Thunar has a very good mass
rename ability.

Blumf

root

unread,
Feb 9, 2012, 12:11:31 PM2/9/12
to
For what it's worth, this is my ren script
for renaming files files and portions of
file names:

#!/bin/sh
if [ ! $1 ] ;then
echo USAGE: $0 [^]oldstring[$] [newstring]
exit
fi
for file in `ls -a|grep $1`
do
simple=$1
simple=${simple/^/}
simple=${simple/$/}
#echo $1 $simple
echo $1 $2
new=${file/$simple/$2}
#echo $file $new
if [ $new ] ;then
mv -i $file $new
fi
done

Floyd L. Davidson

unread,
Feb 9, 2012, 1:53:13 PM2/9/12
to
Grant <o...@grrr.id.au> wrote:
>Recently somebody suggested 'rename' as a solution to something I
>forgot, but after a couple tries 'rename' to remove "_01" from 234
>filenames, I resorted to simple CLI stuff:

Not sure if this is better than other solutions, but the way I've
always done something like that just uses parameter substitution
in the bash shell:

> for i in *_01.JPG ; do mv "${i}" "${i%%_01.JPG}.JPG" ; done

There are a number of pattern matching substitutions possible,
and while that one is the one most often used for filename changes,
sometimes others can also be useful.

--
Floyd L. Davidson <http://www.apaflo.com/>
Ukpeagvik (Barrow, Alaska) fl...@apaflo.com

Grant

unread,
Feb 11, 2012, 8:42:10 AM2/11/12
to
On Thu, 09 Feb 2012 09:53:13 -0900, fl...@apaflo.com (Floyd L. Davidson) wrote:

>Grant <o...@grrr.id.au> wrote:
>>Recently somebody suggested 'rename' as a solution to something I
>>forgot, but after a couple tries 'rename' to remove "_01" from 234
>>filenames, I resorted to simple CLI stuff:
>
>Not sure if this is better than other solutions, but the way I've
>always done something like that just uses parameter substitution
>in the bash shell:
>
> > for i in *_01.JPG ; do mv "${i}" "${i%%_01.JPG}.JPG" ; done

Problem for me is I always forget how the name-chopping (%%) works,
so do it another way to save trying to work out the syntax.
>
>There are a number of pattern matching substitutions possible,
>and while that one is the one most often used for filename changes,
>sometimes others can also be useful.

Many ways, I tend to fall back to simple brute force ways when I
get stuck. Unless I'm trying to optimise for performance.

Grant.

Floyd L. Davidson

unread,
Feb 11, 2012, 8:51:02 AM2/11/12
to
That particular one, using "%%", is the only one that I can always
remember exactly how to use. Half the time with anything else
I have to look it up. But... I use XEmacs, and looking it up
is just another editor command to get the bash man page and then
do a search to get to the right occurance of "%%", from which I
can page up or down to find the others.

Hence from the command line using interactive commands, "%%" is
just about all I use (maybe a substring to chop off the beginning
of a string too). In scripts... I look it up and find the right
way to do it for whatever is needed.

Grant

unread,
Feb 16, 2012, 10:27:33 PM2/16/12
to
On Sat, 11 Feb 2012 04:51:02 -0900, fl...@apaflo.com (Floyd L. Davidson) wrote:

>Grant <o...@grrr.id.au> wrote:
>>On Thu, 09 Feb 2012 09:53:13 -0900, fl...@apaflo.com (Floyd L. Davidson) wrote:
>>
>>>Grant <o...@grrr.id.au> wrote:
>>>>Recently somebody suggested 'rename' as a solution to something I
>>>>forgot, but after a couple tries 'rename' to remove "_01" from 234
>>>>filenames, I resorted to simple CLI stuff:
>>>
>>>Not sure if this is better than other solutions, but the way I've
>>>always done something like that just uses parameter substitution
>>>in the bash shell:
>>>
>>> > for i in *_01.JPG ; do mv "${i}" "${i%%_01.JPG}.JPG" ; done
>>
>>Problem for me is I always forget how the name-chopping (%%) works,
>>so do it another way to save trying to work out the syntax.
>>>
>>>There are a number of pattern matching substitutions possible,
>>>and while that one is the one most often used for filename changes,
>>>sometimes others can also be useful.
>>
>>Many ways, I tend to fall back to simple brute force ways when I
>>get stuck. Unless I'm trying to optimise for performance.
>
>That particular one, using "%%", is the only one that I can always
>remember exactly how to use. Half the time with anything else
>I have to look it up. But... I use XEmacs, and looking it up
>is just another editor command to get the bash man page and then
>do a search to get to the right occurance of "%%", from which I
>can page up or down to find the others.

I start a new terminal to man xxx, need to have that up at same
time as main terminal 'cos short term memory is shot to little bits.
>
>Hence from the command line using interactive commands, "%%" is
>just about all I use (maybe a substring to chop off the beginning
>of a string too). In scripts... I look it up and find the right
>way to do it for whatever is needed.

Fair enough :) I'm not emacs aware user, use vim instead, like the
colour syntax highlighting (desert scheme, the default scheme is
revolting).

Grant.
0 new messages