* Re: help with 'rename' function
[not found] <Pine.SOL.3.95.980612084320.12190A-100000@x139-110.med.umn.edu>
@ 1998-06-12 15:53 ` Timothy J Luoma
1998-06-12 16:21 ` Geoff Wing
0 siblings, 1 reply; 9+ messages in thread
From: Timothy J Luoma @ 1998-06-12 15:53 UTC (permalink / raw)
To: gsker; +Cc: zsh-users
Author: Gerald Skerbitz <gsker@x139-110.med.umn.edu>
Original-Date: Fri, 12 Jun 1998 08:44:34 -0500 (CDT)
Message-ID:
<Pine.SOL.3.95.980612084320.12190A-100000@x139-110.med.umn.edu>
> Am I missing something?
> This seems deceptively easy
>
> for a in *.THIS
> mv $a $a:r.THAT
HRm.... I've never seen the :r before.
Where can I learn more about the "variable:X" usage?
TjL
ps -- yes your answer works just fine for what I want
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: help with 'rename' function
1998-06-12 15:53 ` help with 'rename' function Timothy J Luoma
@ 1998-06-12 16:21 ` Geoff Wing
0 siblings, 0 replies; 9+ messages in thread
From: Geoff Wing @ 1998-06-12 16:21 UTC (permalink / raw)
To: zsh-users
Timothy J Luoma <luomat+Lists/Zsh/users@luomat.peak.org> typed:
:> for a in *.THIS
:> mv $a $a:r.THAT
:HRm.... I've never seen the :r before.
:Where can I learn more about the "variable:X" usage?
:ps -- yes your answer works just fine for what I want
HISTORY EXPANSION/Modifiers. (Not necessarily where you'd expect to find them)
--
Geoff Wing <gcw@pobox.com> Mobile : 0412 162 441
Work URL: http://www.primenet.com.au/ Ego URL: http://pobox.com/~gcw/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: help with 'rename' function
1998-06-12 9:09 ` Peter Stephenson
@ 1998-06-14 20:05 ` Paul Lew
0 siblings, 0 replies; 9+ messages in thread
From: Paul Lew @ 1998-06-14 20:05 UTC (permalink / raw)
To: zsh-users
>>>>> "Peter" == Peter Stephenson <pws@ifh.de> writes:
Peter> Timothy J Luoma wrote:
>> I used to have a binary that did this, but I've lost it.
>>
>> What it did was simple: it changed extensions of filenames.
Peter> It's perennially annoying UNIX doesn't have this. On the
Peter> other hand, if it did it would probably be so inscrutable
Peter> as to be almost useless.
I have been using the DOS style rename under Unix for years, i.e.,
(1)ezdzit lew x>> touch {a,b,c,d,e}.c
(1)ezdzit lew x>> ls
a.c b.c c.c d.c e.c
(1)ezdzit lew x>> ren *.c *.d
(1)ezdzit lew x>> ls
a.d b.d c.d d.d e.d
Yes, this can be done under Unix with some tweak. The following alias
and function definition actually called a csh script to do the real
work which can be converted to zsh. As you can see I have been using
this before the birth of zsh.
alias ren='set -F; rena'
function rena () {
set -F
renam $*
set +F
}
#! /bin/csh -f
#
#- rename - VMS style rename used for wildcard renaming
#-
#- This is to facilitate some desirable VMS feature on Unix. For
#- example:
#- $ mv *.c *.c00
#- $ mv abc.* def.*
#-
#- Another tool 'move' which use sed to change file name is more
#- general purpose than 'rename' or the 'rename.net' written by
#- Juergen Wagner <gandalf@csli.stanford.edu>.
#-
# Author: Paul Lew, General Systems Group, Inc.
# Created at: 03/02/88 01:48 PM
# Last update: 07/29/88 06:01 PM (Edition: 23)
#
#- Usage: rename old new
#- where: there must be one '*' in both old and new
#-
#---------------------------------------------------------------#
# Display help if requested by user #
#---------------------------------------------------------------#
switch ( "$1" )
case -H[xX]:
set echo; set verbose; shift
breaksw
case -H*:
show_help `which $0` $1
goto end
default:
endsw
#---------------------------------------------------------------#
# Process Arguments #
#---------------------------------------------------------------#
if ( $#argv != 2 ) then
show_help `which $0`
goto end
endif
#---------------------------------------------------------------#
# Process each item in the argument list #
#---------------------------------------------------------------#
set src = "$1"
set dest = "$2"
set cmd
set noglob
if ( ( "$1:r" != '*' && "$1:e" != '*' ) || \
( "$2:r" != '*' && "$2:e" != '*' ) ) then
echo "wildcard characters required on both src and dest"
goto end
endif
if ( "$src:r" == "$dest:r" && "$src:r" == '*' ) then
set same_root
else if ( "$src:e" == "$dest:e" && "$src:e" == '*' ) then
set same_ext
else if ( "$src:r" == "$dest:e" && "$src:r" == '*' ) then
set r_to_e
else if ( "$src:e" == "$dest:r" && "$src:e" == '*' ) then
set e_to_r
endif
unset noglob
set nonomatch
foreach file ($src)
if ( "$file" == "$src" ) then
echo "...File: $file not found, aborted..."
goto end
endif
if ( $?same_root ) $cmd mv $file ${file:r}.${dest:e}
if ( $?same_ext ) $cmd mv $file ${dest:r}.${file:e}
if ( $?r_to_e ) $cmd mv $file ${dest:r}.${file:r}
if ( $?e_to_r ) $cmd mv $file ${file:e}.${dest:e}
end
#---------------------------------------------------------------#
# Clean up and exit here... #
#---------------------------------------------------------------#
end:
unset src dest file
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: help with 'rename' function
1998-06-12 8:15 Timothy J Luoma
` (3 preceding siblings ...)
1998-06-12 9:55 ` Hannu Koivisto
@ 1998-06-12 23:07 ` Thomas Köhler
4 siblings, 0 replies; 9+ messages in thread
From: Thomas Köhler @ 1998-06-12 23:07 UTC (permalink / raw)
To: zsh-users
Hello,
On Fri, Jun 12, 1998 at 04:15:57AM -0400, Timothy J Luoma wrote:
>
> I used to have a binary that did this, but I've lost it.
>
> What it did was simple: it changed extensions of filenames.
>
> For example, say you have a bunch of files which ended with .THIS and you
> wanted to change them to end with .THAT
>
> You would do
>
> rename *.THIS THIS=THAT
>
> and it would go on its way... or if it was just one file:
>
> rename foo.THIS THIS=THAT
Apart from using mmv (I think it should be somewhere on
sunsite/mirrors), why not use this one:
for i in *.THIS ; do mv $i `basename $i .THIS`.THAT ; done
CU,
Thomas
--
Thomas Köhler Email: jean-luc@picard.franken.de
<>< WWW: http://home.pages.de/~jeanluc/
IRC: jeanluc
LCARS --- Linux for Computers on All Real Starships
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: help with 'rename' function
1998-06-12 8:15 Timothy J Luoma
` (2 preceding siblings ...)
1998-06-12 9:26 ` Zefram
@ 1998-06-12 9:55 ` Hannu Koivisto
1998-06-12 23:07 ` Thomas Köhler
4 siblings, 0 replies; 9+ messages in thread
From: Hannu Koivisto @ 1998-06-12 9:55 UTC (permalink / raw)
To: zsh-users
Timothy J Luoma <luomat+Lists/Zsh/users@luomat.peak.org> writes:
| For example, say you have a bunch of files which ended with .THIS and you
| wanted to change them to end with .THAT
|
| You would do
|
| rename *.THIS THIS=THAT
|
| and it would go on its way... or if it was just one file:
|
| rename foo.THIS THIS=THAT
Well, I needed to do exactly this but also do other arbitrary
rename operations to one or many files, so I first created a
"higher-order" ;) function...
rename-with-filter () {
if ! [[ $# -lt 2 ]]
then
for kala in $argv[2,-1]
do
mv "$kala" "$(echo "$kala" | eval $1)"
done
fi
}
...using which I could make, for example, a following kind of
function to transform filenames like ThisIsFilename to
This_Is_Filename:
rename_separate () {
rename-with-filter "sed -e 's/\([a-z]\)\([A-Z]\)/\1_\2/g'" $*
}
This should handle your much simpler case too.
| I've tried to make a function which does this, but it fails:
Sorry, can't help you with your implementation, I'm pretty much
write-only zsh user :)
//Hannu
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: help with 'rename' function
1998-06-12 8:15 Timothy J Luoma
1998-06-12 9:09 ` Peter Stephenson
1998-06-12 9:14 ` Bart Schaefer
@ 1998-06-12 9:26 ` Zefram
1998-06-12 9:55 ` Hannu Koivisto
1998-06-12 23:07 ` Thomas Köhler
4 siblings, 0 replies; 9+ messages in thread
From: Zefram @ 1998-06-12 9:26 UTC (permalink / raw)
To: Timothy J Luoma; +Cc: zsh-users
Timothy J Luoma wrote:
>You would do
>
> rename *.THIS THIS=THAT
function rename {
emulate zsh
setopt localoptions
integer st=0
local pat old new f
if (( $# < 2 )) || { pat=$argv[$#]; [[ $pat != *\=* ]]; }; then
echo >&2 "Usage: rename <files...> <foo>=<bar>"
return 2
fi
old=${pat%%\=*}
new=${pat#*\=}
for f in $argv[1,-2]; do
if [[ $f != *$old ]]; then
st=1
echo >&2 "$f: does not end with suffix \`$old'"
elif ! mv $f ${f%$old}$new; then
st=1
fi
done
return $st
}
If you use the files module, then this function does not use any external
commands.
-zefram
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: help with 'rename' function
1998-06-12 8:15 Timothy J Luoma
1998-06-12 9:09 ` Peter Stephenson
@ 1998-06-12 9:14 ` Bart Schaefer
1998-06-12 9:26 ` Zefram
` (2 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 1998-06-12 9:14 UTC (permalink / raw)
To: Timothy J Luoma, zsh-users
On Jun 12, 4:15am, Timothy J Luoma wrote:
} Subject: help with 'rename' function
}
} I used to have a binary that did this, but I've lost it.
}
} What it did was simple: it changed extensions of filenames.
}
} For example, say you have a bunch of files which ended with .THIS and you
} wanted to change them to end with .THAT
}
} You would do
}
} rename *.THIS THIS=THAT
I've had the following sitting around for a -long- time; in fact, I seem
not to have used it since sometime before installing 3.0, as I had to fix
some things to make it work. It should probably be rewritten to use the
"getopts" builtin, and there's probably other things that are being done
in old-fashioned ways.
I have this in a file named "ren" in a directory in my fpath. Probably
best invoked as "noglob ren ...." rather than with all the glob patterns
quoted (as they are in the examples). Note that the patterns are glued
together as strings, without first checking for directory-ness etc., so
you have to be careful of where you put your "/" characters.
I doubt the core dump mentioned in the comments below still afflicts zsh,
but ....
#! /usr/local/bin/zsh -f
#
# Rename files according to a pattern.
#
# ren \*.foo .bar Renames all *.foo files to *.bar
# ren -h foo\* bar Renames all foo* files to bar*
# ren -c cp \*.foo .bar Copies all *.foo files to *.bar
# ren \*.foo .bar /tmp/ Renames *.foo files to /tmp/*.bar
# ren /tmp/\*.bar .foo /usr/tmp/ Renames /tmp/*.bar to /usr/tmp/*.foo
# ren -h /tmp/bar\* /foo /usr/tmp Renames /tmp/bar* to /usr/tmp/foo*
# ren -h /tmp/bar\* /usr/tmp/foo Renames /tmp/bar* to /usr/tmp/foo*
#
# To check what ren will execute, use something like:
# ren -c echo /tmp/\*.bar .foo /usr/tmp
#
# (Note, the above example has a deliberate error, missing a /.)
#
# To use I/O redirection instead of arguments ("filter" mode):
# ren -c head\< /tmp/\*bar .foo \>
# ren -c cat\< /tmp/\*bar .foo \>/usr/tmp/
#
# The -e option (error) causes ren to exit if any command fails.
#
# The -s option (swap) switches the positions of the pattern and the
# replacement:
#
# ren -s \*.foo .bar For all *.foo renames .bar to .foo
#
# The -x option (eXchange) executes the command only on the replacements
# (i.e., passes only one argument to the command, which is the replacement):
#
# ren -x -c touch \*.o .c For each *.o, touches the .c
local files="" prefix="" chop=% head="" tail="" cmd=mv error="" xchg=false i
while [[ $# -gt 2 ]]
do
case $1 in
-c) cmd=$2; shift 2;;
-e) error='|| break';;
-h) chop='#*'; shift;;
-t) chop='%'; shift;;
-s) xchg=true; shift;;
-x) xchg=drop; shift;;
*) break;;
esac
done
if [[ $# -lt 2 || $# -gt 3 || -z "$cmd" ]]
then
echo "usage: ${0:t} [-h|-t] [-e] [-c command] pat repl [prefix]" 1>&2
if [[ $# -gt 3 ]]
then
echo 'Did you remember to quote metacharacters in the pattern?' 1>&2
fi
return -1
elif [[ $# -eq 3 ]]
then
prefix=$3
fi
case $chop in
%) tail=$2;;
*) head=$2;;
esac
# Use globbing to get the target files
files=($~1)
for i in $files
do
case $xchg in
drop)
eval $cmd $prefix$head\$\{\$\{i$chop$1:t\}:r\}$tail $error
;;
true)
eval $cmd $prefix$head\$\{\$\{i$chop$1:t\}:r\}$tail $i $error
;;
*)
eval $cmd $i $prefix$head\$\{\$\{i$chop$1:t\}:r\}$tail $error
;;
esac # Omit this and zsh dumps core in gettext2()
done
--
Bart Schaefer Brass Lantern Enterprises
http://www.well.com/user/barts http://www.brasslantern.com
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: help with 'rename' function
1998-06-12 8:15 Timothy J Luoma
@ 1998-06-12 9:09 ` Peter Stephenson
1998-06-14 20:05 ` Paul Lew
1998-06-12 9:14 ` Bart Schaefer
` (3 subsequent siblings)
4 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 1998-06-12 9:09 UTC (permalink / raw)
To: Timothy J Luoma, Zsh users list
Timothy J Luoma wrote:
> I used to have a binary that did this, but I've lost it.
>
> What it did was simple: it changed extensions of filenames.
It's perennially annoying UNIX doesn't have this. On the other hand,
if it did it would probably be so inscrutable as to be almost useless.
If you want a proper programme, search the archives for mmv. There's
also a widely distributed perl script called rename which allows perl
operations on the file name, e.g. 's/\.this$/.that/' in this case.
(I can mail my version.)
If you want a shell function, it's appended...
> For example, say you have a bunch of files which ended with .THIS and you
> wanted to change them to end with .THAT
>
> You would do
>
> rename *.THIS THIS=THAT
...the difference is it was easier to stick the THIS=THAT argument
first rather than last. Everything's done by the shell, so it's
a bit simpler. It's limited to suffixes as written. Note the -f
if you want to overwrite an existing file.
# rename.zsh
local force
[[ $1 = -f ]] && shift && force=1
if [[ $# -lt 2 || $1 != *=* ]]; then
print "Usage: $0 oldsuf=newsuf file ..." 2>&1
fi
local old new ofile nfile
old=${1%=*}
new=${1#*=}
shift
for ofile in $*; do
nfile=${ofile%$old}$new
if [[ -f $nfile && $force != 1 ]]; then
print "$nfile already exists" 2>&1
else
mv $ofile $nfile
fi
done
# end
--
Peter Stephenson <pws@ifh.de> Tel: +39 50 844536
WWW: http://www.ifh.de/~pws/
Gruppo Teorico, Dipartimento di Fisica
Piazza Torricelli 2, 56100 Pisa, Italy
^ permalink raw reply [flat|nested] 9+ messages in thread
* help with 'rename' function
@ 1998-06-12 8:15 Timothy J Luoma
1998-06-12 9:09 ` Peter Stephenson
` (4 more replies)
0 siblings, 5 replies; 9+ messages in thread
From: Timothy J Luoma @ 1998-06-12 8:15 UTC (permalink / raw)
To: zsh-users
I used to have a binary that did this, but I've lost it.
What it did was simple: it changed extensions of filenames.
For example, say you have a bunch of files which ended with .THIS and you
wanted to change them to end with .THAT
You would do
rename *.THIS THIS=THAT
and it would go on its way... or if it was just one file:
rename foo.THIS THIS=THAT
I've tried to make a function which does this, but it fails:
(the $others variable is ALL the file names rather than cycling through one by one)
rename () {
local i last oldext newext short new others
last=`echo $* | awk '{print $NF}'`
# all the args except the last one
others=`echo $* | sed "s/ $last$//g"`
oldext=`echo $rename | tr -s '=' ' ' | awk '{print $1}'`
newext=`echo $rename | tr -s '=' ' ' | awk '{print $2}'`
echo "oldext is $oldext"
echo "newext is $newext"
for i in $others
do
short=`echo $i .$oldext`
new=`echo $short.$newext`
mv $i $new
done
}
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~1998-06-14 20:14 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <Pine.SOL.3.95.980612084320.12190A-100000@x139-110.med.umn.edu>
1998-06-12 15:53 ` help with 'rename' function Timothy J Luoma
1998-06-12 16:21 ` Geoff Wing
1998-06-12 8:15 Timothy J Luoma
1998-06-12 9:09 ` Peter Stephenson
1998-06-14 20:05 ` Paul Lew
1998-06-12 9:14 ` Bart Schaefer
1998-06-12 9:26 ` Zefram
1998-06-12 9:55 ` Hannu Koivisto
1998-06-12 23:07 ` Thomas Köhler
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).