home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / tutorial / eg / rename < prev    next >
Encoding:
Text File  |  1990-12-17  |  2.5 KB  |  117 lines

  1. #!/usr/bin/perl
  2. 'di';
  3. 'ig00';
  4. #
  5. # $Header: rename,v 3.0.1.2 90/08/09 03:17:57 lwall Locked $
  6. #
  7. # $Log:    rename,v $
  8. # Revision 3.0.1.2  90/08/09  03:17:57  lwall
  9. # patch19: added man page for relink and rename
  10.  
  11. if ($ARGV[0] eq '-i') {
  12.     shift;
  13.     if (open(TTYIN, "</dev/tty") && open(TTYOUT,">/dev/tty")) {
  14.     $inspect++;
  15.     select((select(TTYOUT),$|=1)[0]);
  16.     } 
  17. }
  18. ($op = shift) || die "Usage: rename [-i] perlexpr [filenames]\n";
  19. if (!@ARGV) {
  20.     @ARGV = <STDIN>;
  21.     chop(@ARGV);
  22. }
  23. for (@ARGV) {
  24.     unless (-e) {
  25.     print STDERR "$0: $_: $!\n";
  26.     $status = 1;
  27.     next;
  28.     } 
  29.     $was = $_;
  30.     eval $op;
  31.     die $@ if $@;
  32.     if ($was ne $_) {
  33.     if ($inspect && -e) {
  34.         print TTYOUT "remove $_? ";
  35.         next unless <TTYIN> =~ /^y/i;
  36.     } 
  37.     unless (rename($was, $_)) {
  38.         print STDERR "$0: can't rename $was to $_: $!\n";
  39.         $status = 1;
  40.     }
  41.     } 
  42. }
  43. exit $status;
  44. ##############################################################################
  45.  
  46.     # These next few lines are legal in both Perl and nroff.
  47.  
  48. .00;            # finish .ig
  49.  
  50. 'di            \" finish diversion--previous line must be blank
  51. .nr nl 0-1        \" fake up transition to first page again
  52. .nr % 0            \" start at page 1
  53. ';<<'.ex'; #__END__ ############# From here on it's a standard manual page ############
  54. .TH RENAME 1 "July 30, 1990"
  55. .AT 3
  56. .SH NAME
  57. rename \- renames multiple files
  58. .SH SYNOPSIS
  59. .B rename [-i] perlexpr [files]
  60. .SH DESCRIPTION
  61. .I Rename
  62. renames the filenames supplied according to the rule specified as the
  63. first argument.
  64. The argument is a Perl expression which is expected to modify the $_
  65. string in Perl for at least some of the filenames specified.
  66. If a given filename is not modified by the expression, it will not be
  67. renamed.
  68. If no filenames are given on the command line, filenames will be read
  69. via standard input.
  70. .PP
  71. The 
  72. .B \-i
  73. flag will prompt to remove the old file first if it exists.  This
  74. flag will be ignored if there is no tty.
  75. .PP
  76. For example, to rename all files matching *.bak to strip the extension,
  77. you might say
  78. .nf
  79.  
  80.     rename 's/\e.bak$//' *.bak
  81.  
  82. .fi
  83. To translate uppercase names to lower, you'd use
  84. .nf
  85.  
  86.     rename 'y/A-Z/a-z/' *
  87.  
  88. .fi
  89. To do the same thing but leave Makefiles unharmed:
  90. .nf
  91.  
  92.     rename 'y/A-Z/a-z/ unless /^Make/' *
  93.  
  94. .fi
  95. To rename all the *.f files to *.BAD, you'd use
  96. .nf
  97.  
  98.     rename 's/\e.f$/.BAD/' *.f
  99.  
  100. .SH ENVIRONMENT
  101. .fi
  102. No environment variables are used.
  103. .SH FILES
  104. .SH AUTHOR
  105. Larry Wall
  106. .SH "SEE ALSO"
  107. mv(1)
  108. .br
  109. perl(1)
  110. .SH DIAGNOSTICS
  111. If you give an invalid Perl expression you'll get a syntax error.
  112. .SH BUGS
  113. .I Rename
  114. does not check for the existence of target filenames, so use with care.
  115. .ex
  116.