home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10216 < prev    next >
Encoding:
Text File  |  1992-08-20  |  2.0 KB  |  71 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!usc!sol.ctr.columbia.edu!ira.uka.de!math.fu-berlin.de!zrz.tu-berlin.de!mailgzrz.tu-berlin.de!nickel
  3. From: nickel@cs.tu-berlin.de (Juergen Nickelsen)
  4. Subject: Re: Changing file names
  5. In-Reply-To: bosak@netcom.com's message of Thu, 20 Aug 92 06:18:14 GMT
  6. Message-ID: <NICKEL.92Aug20174909@desaster.cs.tu-berlin.de>
  7. Lines: 56
  8. Sender: news@mailgzrz.tu-berlin.de (News Manager)
  9. Nntp-Posting-Host: desaster.cs.tu-berlin.de
  10. Reply-To: nickel@cs.tu-berlin.de
  11. Organization: STONE Project, Technical University of Berlin, Germany
  12. References: <ac#n06l.bosak@netcom.com>
  13. Date: Thu, 20 Aug 1992 15:49:12 GMT
  14.  
  15. In article <ac#n06l.bosak@netcom.com> bosak@netcom.com (Jon Bosak)
  16. writes:
  17.  
  18. > Suppose I have a directory containing files named, for example,
  19. >
  20. >       xyz920815a
  21. [...]
  22. >       xyz920818b
  23. >
  24. > ...and so on.  I want to change the names of these files to
  25. >
  26. >       920815a
  27. [...]
  28. >       920818b
  29. >
  30. > ...and so on.  Simple, right?  Just chop off the first three
  31. > characters and there you are.  But how is it done?
  32.  
  33. There have some solutions been posted, which are undoubtedly suited
  34. for this task, but all of them used non-standard tools -- nawk, perl,
  35. ksh. This isn't necessary, it can be done with even the most basic
  36. UNIX tools -- sh, sed, mv. I guess there isn't any reasonable UNIX
  37. system delivered without these.
  38.  
  39. This is my ~/bin/rename:
  40. ---------cut here----------cut here------------cut here---------------
  41. #!/bin/sh
  42. # rename files using an sed command.
  43. # common usage: rename s/from/to/ 
  44.  
  45. iflag=
  46. case "$1" in
  47.     -i)    iflag=-i
  48.     shift ;;
  49. esac
  50.  
  51. if [ $# -le 1 ] ; then
  52.     echo Usage: rename sed-command file ...
  53.     exit 1
  54. fi
  55.  
  56. regexp="$1"
  57. shift
  58. for i in "$@" ; do
  59.     if [ "$i" != "`echo $i | sed \"$regexp\"`" ] ; then
  60.     mv $iflag "$i" "`echo $i | sed \"$regexp\"`"
  61.     fi
  62. done
  63. ---------cut here----------cut here------------cut here---------------
  64.  
  65. A still remaining problem is the quoting of funny characters in the
  66. argument strings -- there are probably some examples still defeating
  67. this script.
  68.  
  69. --
  70. Juergen Nickelsen
  71.