home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!usc!sol.ctr.columbia.edu!ira.uka.de!math.fu-berlin.de!zrz.tu-berlin.de!mailgzrz.tu-berlin.de!nickel
- From: nickel@cs.tu-berlin.de (Juergen Nickelsen)
- Subject: Re: Changing file names
- In-Reply-To: bosak@netcom.com's message of Thu, 20 Aug 92 06:18:14 GMT
- Message-ID: <NICKEL.92Aug20174909@desaster.cs.tu-berlin.de>
- Lines: 56
- Sender: news@mailgzrz.tu-berlin.de (News Manager)
- Nntp-Posting-Host: desaster.cs.tu-berlin.de
- Reply-To: nickel@cs.tu-berlin.de
- Organization: STONE Project, Technical University of Berlin, Germany
- References: <ac#n06l.bosak@netcom.com>
- Date: Thu, 20 Aug 1992 15:49:12 GMT
-
- In article <ac#n06l.bosak@netcom.com> bosak@netcom.com (Jon Bosak)
- writes:
-
- > Suppose I have a directory containing files named, for example,
- >
- > xyz920815a
- [...]
- > xyz920818b
- >
- > ...and so on. I want to change the names of these files to
- >
- > 920815a
- [...]
- > 920818b
- >
- > ...and so on. Simple, right? Just chop off the first three
- > characters and there you are. But how is it done?
-
- There have some solutions been posted, which are undoubtedly suited
- for this task, but all of them used non-standard tools -- nawk, perl,
- ksh. This isn't necessary, it can be done with even the most basic
- UNIX tools -- sh, sed, mv. I guess there isn't any reasonable UNIX
- system delivered without these.
-
- This is my ~/bin/rename:
- ---------cut here----------cut here------------cut here---------------
- #!/bin/sh
- # rename files using an sed command.
- # common usage: rename s/from/to/
-
- iflag=
- case "$1" in
- -i) iflag=-i
- shift ;;
- esac
-
- if [ $# -le 1 ] ; then
- echo Usage: rename sed-command file ...
- exit 1
- fi
-
- regexp="$1"
- shift
- for i in "$@" ; do
- if [ "$i" != "`echo $i | sed \"$regexp\"`" ] ; then
- mv $iflag "$i" "`echo $i | sed \"$regexp\"`"
- fi
- done
- ---------cut here----------cut here------------cut here---------------
-
- A still remaining problem is the quoting of funny characters in the
- argument strings -- there are probably some examples still defeating
- this script.
-
- --
- Juergen Nickelsen
-