From: "Bart Schaefer" <schaefer@brasslantern.com>
To: Timothy J Luoma <luomat+Lists/Zsh/users@luomat.peak.org>,
zsh-users@math.gatech.edu
Subject: Re: help with 'rename' function
Date: Fri, 12 Jun 1998 02:14:35 -0700 [thread overview]
Message-ID: <980612021435.ZM20682@candle.brasslantern.com> (raw)
In-Reply-To: <199806120816.EAA22173@luomat.peak.org>
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
next prev parent reply other threads:[~1998-06-12 9:17 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
1998-06-12 9:26 ` Zefram
1998-06-12 9:55 ` Hannu Koivisto
1998-06-12 23:07 ` Thomas Köhler
1998-06-13 11:02 ` rename THIS=THAT Sven Guckes
[not found] ` <Pine.SOL.3.95.980613090415.15614A-100000@x139-110.med.umn.edu>
1998-06-15 0:37 ` zsh - rename THIS=THAT - mv $a $a:s/THIS/THAT/ Sven Guckes
[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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=980612021435.ZM20682@candle.brasslantern.com \
--to=schaefer@brasslantern.com \
--cc=luomat+Lists/Zsh/users@luomat.peak.org \
--cc=zsh-users@math.gatech.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).