| 
     
       |  |  |  | 
            
              | 
             
               | 
               
                 |  | UGU: Unix Guru Universe - Unix Tip #4474- June 11, 2025
 -  Home : Help
 : Today's Tip Unix Tip #4474- June 11, 2025
 
 
Rename files script 
If you want to rename files
 in a directory then you can
 use the following perl
 script....
 
 #!/usr/bin/perl
 # rename: renames files according to the expr given on the command line.
 # The expr will usually be a 's' or 'y' command, but can be any valid
 # perl command if it makes sense. Takes a list of files to work on or
 # defaults to '*' in the current directory.
 # e.g.
 #       rename 's/\.flip$/.flop/'       # rename *.flip to *.flop
 #       rename s/flip/flop/             # rename *flip* to *flop*
 #       rename 's/^s\.(.*)/$1.X/'       # switch sccs filenames around
 #       rename 's/$/.orig/' */*.[ch]    # add .orig to your source files in */
 #       rename 'y/A-Z/a-z/'             # lowercase all filenames in .
 #       rename 'y/A-Z/a-z/ if -B'       # same, but just binaries!
 #       rename chop *~                  # restore all ~ backup files
 
 use Getopt::Std;
 my ($subst, $name);
 
 if (!&getopts("nfq") || $#ARGV == -1) {
 die "Usage: rename [-fnq]  [file file...]
 -f   : Force the new filename even if it exists already
 -n   : Just print what would happen, but don't do the command
 -q   : Don't print the files as they are renamed
 e.g. : rename  's/\.c/.c.old/' *
 rename -q 'y/A-Z/a-z/' *
";
 }
 
 $subst = shift;                 # Get perl command to work on
 @ARGV = <*> if $#ARGV < 0;      # Default to complete directory
 
 foreach $name (@ARGV) {
 $_ = $name;
 eval "$subst;";
 die $@ if $@;
 next if -e $_ && !$opt_f;   # Skip if the file exists if asked to.
 mext if $_ eq $name;
 if ($opt_n) {
 print "mv $name $_
";
 next;
 }
 print "mv $name $_
" if !$opt_q;
 rename($name,$_) or warn "Can't rename $name to $_, $!
";
 }
 
 Put the script called rename in /usr/local/bin. Make sure
 /usr/local/bin is in your $PATH for convenience.
 
 
 This tip generously supported by: dave.ruddle@siemens.co.uk
 
 
 
 NOTE: All tips provided are USE AT YOUR OWN RISK. Tips are submitted
by various unix admins around the globe. UGU suggest you read and
test each tip in a non-volitile environment before placing into
production.
 
 LAST 5 TIPS
 4473
 - BOURNE SHELL PSEUDO HASH TABLES
 4472
 - BE A GURU - RTFM
 4471
 - ONE STEP AT A TIME WITH !!
 4470
 - PRINTING WITH NO SPACE
 4469
 - To grab all hidden files
 I want to 
 SUBSCRIBE
 and get a UGU Tip everyday.
 I want to 
 UNSUBSCRIBE
 and NOT get a UGU Tip everyday.
 If you have a UNIX TIP let us know, we just may use it:
(All tips become the property of the Unix Guru Universe)
 |  |  
                 |  |  
                 |   Copyright 1994-2024 Unix Guru Universe 
 |  |  |  |  |