Renaming lots of files

Renaming lots of files

Post by M.J.Fo » Wed, 11 May 1994 21:17:40



Hi,
    I have a lot of files with names that differ only
by a digit at the end,

Eg.   res1.pl, res2.pl, etc

What I want to do is rename the file all at once,

Eg.   res1.pl  becomes data1.pl  etc

I hope this is clear.
Thanks for any tips.

Martin

 
 
 

Renaming lots of files

Post by Timothy Kimba » Wed, 11 May 1994 21:31:40


:     I have a lot of files with names that differ only
: by a digit at the end,
: Eg.   res1.pl, res2.pl, etc
: What I want to do is rename the file all at once,
: Eg.   res1.pl  becomes data1.pl  etc

I'm sure there are slicker solutions, but I usually
knock out a quick shell script for this:

-------------------------------------------------
#!/bin/sh
for oldname in res*
do
        newname=`echo $oldname | sed 's/^res/data/'`
        echo Moving $oldname to $newname
        mv $oldname $newname
done
-------------------------------------------------

Comment out the mv line first, to make sure it has
the names right.

-- tdk

 
 
 

Renaming lots of files

Post by B.N.Blackmo » Wed, 11 May 1994 23:31:40



> Hi,
> I have a lot of files with names that differ only
> by a digit at the end,
> Eg.   res1.pl, res2.pl, etc
> What I want to do is rename the file all at once,
> Eg.   res1.pl  becomes data1.pl  etc
> I hope this is clear. Thanks for any tips.

From Larry and Randals great Perl book "Programming Perl" (insert
subliminal buy this book messages) comes the following dead simple
perl script.

#!/usr/bin/perl

# Usage: rename perlexpr [files]

($opt = shift) || die "Usgae: rename perlexpr [filenames]\n";



Quote:}


        $was = $_;
        eval $op;

        rename($was,$_) unless $was eq $_;
Quote:}

__END__

To use say something like

rename 's/res/data/' *

--
Graduating soon, and no job yet to go to, all UK offers gratefully
considered, email above address for details and my CV or miss out.

 
 
 

Renaming lots of files

Post by Patrick Seema » Wed, 11 May 1994 23:16:13


  Martin> What I want to do is rename the file all at once,
  Martin> Eg.  res1.pl becomes data1.pl etc

Using sh:
for old in *.pl; do
    mv $old `echo $old | sed s/res/data/`
done

Of course, there's always a perl solution :-)

greetings, pat
--
Don't even try to reply to the address above. If you want to send me email,

And no, I do not speak for UBS, I just work here.

 
 
 

Renaming lots of files

Post by Willy Seibe » Thu, 12 May 1994 00:46:59


|> Hi,
|>     I have a lot of files with names that differ only
|> by a digit at the end,
|>
|> Eg.   res1.pl, res2.pl, etc
|>
|> What I want to do is rename the file all at once,
|>
|> Eg.   res1.pl  becomes data1.pl  etc

This is a special solution for Martin's special problem:

#!/bin/sh

for i in res*.pl
do
        digit=`echo $i | tr -d '[a-zA-Z.]' `
        mv $i data${digit}.pl
done

--
---

-       * Heaven give me words - Heaven let this message reach You *
                        Jung vf guvf ohggba sbe ?

 
 
 

Renaming lots of files

Post by Sriram Srinivas » Thu, 12 May 1994 00:47:01



> What I want to do is rename the file all at once,

Use the perl script "rename" (given in the Camel book), or get this utility
called mmv (multiple mv) from comp.sources.misc - nice tool.

Sriram

 
 
 

Renaming lots of files

Post by Srinivas Go » Wed, 11 May 1994 23:58:56


--
Here is a script I got on this newsgroup. It has been very usefull to me.

#! /bin/sh

PURPOSE="For all filenames in the current directory, \nnew_substr is replaced for old_substr"

PROG=`basename $0`
Usage () {
        echo "Usage: $PROG [-f] old_substr new_substr"
 echo "$PURPOSE"

Quote:}

if [ "$1" = "-f" ]
then
 ASK=no
 shift
else
 ASK=yes
fi

if      [ $# != 2 ]
then    Usage
        exit 1
fi

for oldname in *${1}*
do
 newname=`echo $oldname | sed -e 's|'$1'|'$2'|'`

 if [ -f $newname ]
 then if [ "$ASK" = "no" ]
  then
   echo "renaming $oldname into $newname"
   mv $oldname $newname
  else
   echo "Warning: File $newname already exists"
   echo "(Use the -f flag to avoid this check)"
   echo "Overwrite (y/n)? \c"
   read answer
   case $answer in
    y*|Y*)
     echo "renaming $oldname into $newname"
     mv $oldname $newname
     ;;
   esac
  fi
 else  #does not yet exist
  echo "renaming $oldname into $newname"
  mv $oldname $newname
 fi
done

 
 
 

Renaming lots of files

Post by Mark Brad » Sat, 14 May 1994 17:47:00


Quote:>  > What I want to do is rename the file all at once,
>  > Eg.  res1.pl becomes data1.pl etc

> for old in *.pl; do
>     mv $old `echo $old | sed s/res/data/`
> done

Well, it works, but why fork a sed for every file?

  ls res*.pl | sed -e 's/.*/mv &  &/' -e 's/  res/ data/' | sh
--

SoftQuad Inc., Toronto   |   Or perhaps just mad!"    -- Robert L. Biddle

This article is in the public domain.

 
 
 

Renaming lots of files

Post by Paul Ju » Mon, 16 May 1994 08:30:11


   >  > What I want to do is rename the file all at once,
   >  > Eg.  res1.pl becomes data1.pl etc
   >
   > for old in *.pl; do
   >     mv $old `echo $old | sed s/res/data/`
   > done

I like the following script, try it:

        move s/res/data/ *.pl

There is also as similar version written in perl call 'rename', but
you have to have perl...

#! /bin/sh
#
#-      move - rename files use sed
#-
#-      Move will  rename all   the  files  in the argument   list (or
#-      standard input if  no  file  is specified) based   on  the sed
#-      command given.   A  typical sed  command will  be:  s/old/new/
#-      which replace the old string in each of the  filename with the
#-      new string.  Metacharacter works as usual: ^ matches beginning
#-      of filename, $ matches the end of filename, etc.
#-
#       Author:         Paul Lew, General Systems Group, Inc.
#       Created at:     07/26/88  11:03 AM
#       Last update:    07/26/88  12:45 PM  (Edition: 24)
#
#-      Usage:          move [-n] [-x] sed_cmd [file1 file2 ...]   or
#-                      move [-n] [-x] [-l] [-u] [file1 file2 ...]
#-      where options:
#-                      -n      show what will be executed.
#-                      -x      show command while it executes.
#-                      -l      convert all name to lowercase
#-                      -u      convert all name to uppercase
#
#---------------------------------------------------------------#
#             Display help if requested by user                 #
#---------------------------------------------------------------#
case "$1" in
        -H[xX]) set -x; shift;;
        -H*)    show_help `which $0` $1; exit;;
        *)      ;;
        esac
#---------------------------------------------------------------#
#                       Process Arguments                       #
#---------------------------------------------------------------#
prgname=$0 opt= cmd=sh ncase= sed_cmd=
while true; do
        case "$1" in
                -x) opt='-x'; shift;;
                -n) cmd=cat; shift;;
                -u) shift;
                    ncase='s/a/A/g;s/b/B/g;s/c/C/g;s/d/D/g;s/e/E/g;
                           s/f/F/g;s/g/G/g;s/h/H/g;s/i/I/g;s/j/J/g;
                           s/k/K/g;s/l/L/g;s/m/M/g;s/n/N/g;s/o/O/g;
                           s/p/P/g;s/q/Q/g;s/r/R/g;s/s/S/g;s/t/T/g;
                           s/u/U/g;s/v/V/g;s/w/W/g;s/x/X/g;s/y/Y/g;
                           s/z/Z/g';;
                -l) shift;
                    ncase='s/A/a/g;s/B/b/g;s/C/c/g;s/D/d/g;s/E/e/g;
                           s/F/f/g;s/G/g/g;s/H/h/g;s/I/i/g;s/J/j/g;
                           s/K/k/g;s/L/l/g;s/M/m/g;s/N/n/g;s/O/o/g;
                           s/P/p/g;s/Q/q/g;s/R/r/g;s/S/s/g;s/T/t/g;
                           s/U/u/g;s/V/v/g;s/W/w/g;s/X/x/g;s/Y/y/g;
                           s/Z/z/g';;
                *) break;;
                esac
        done
if [ "_$ncase" = "_" ]; then
        if [ $# -lt 1 ]; then
                show_help `which $prgname`
                echo -n "Enter sed command: "
                read sed_cmd
                if [ "_$sed_cmd" = "_" ]; then exit; fi
        else
                sed_cmd="$1"
                shift
                fi
        fi
#---------------------------------------------------------------#
#           Process each item in the argument list              #
#---------------------------------------------------------------#
if [ "_$*" = "_" ]; then
        cat
else
        echo $* | tr ' ' '\012'
        fi |
sed -e 'h;'"$sed_cmd;$ncase"';x;G;s/^/mv /;s/\n/ /' | \
$cmd $opt
#---------------------------------------------------------------#
#               Clean up and exit here...                       #
#---------------------------------------------------------------#
--


 
 
 

Renaming lots of files

Post by Goldmann Ferdina » Sat, 29 Oct 1994 01:01:49


: Hi,
:     I have a lot of files with names that differ only
: by a digit at the end,

: Eg.   res1.pl, res2.pl, etc

: What I want to do is rename the file all at once,

: Eg.   res1.pl  becomes data1.pl  etc

: I hope this is clear.
: Thanks for any tips.

: Martin

use a perl script... the method you can use is described in
'Programming Perl'. section "common tasks" I think.

Regards,
/ferdl

--
###########################################################################
# In Real Life:  Goldmann Ferdinand                   --= LinuX rules =-- #
# Address     :  Bluetenstr. 23/36                     --= the world =--  #
#                4040 Linz/Austria                                        #
# IRC         :  DrAkHaI 8^)                                              #


###########################################################################

 
 
 

Renaming lots of files

Post by Shane Cast » Sat, 29 Oct 1994 07:27:50


In comp.unix.shell,



>:     I have a lot of files with names that differ only
>: by a digit at the end,
>: Eg.   res1.pl, res2.pl, etc
>: What I want to do is rename the file all at once,
>: Eg.   res1.pl  becomes data1.pl  etc
>use a perl script... the method you can use is described in
>'Programming Perl'. section "common tasks" I think.

Or, in Bourne shell:

ls -1 *[0-9].pl | while read fn;do nfn=`echo $fn | sed 's/res/data/'`; \
mv $fn $nfn;done

WARNING: don't copy these lines directly and use them; the '\' may
not be valid at the end of the first line.

--

Boulder County Info Svcs
Boulder CO USA

 
 
 

1. renaming lots of files

Hi.

I need to rename lots of files in one of my directories.   I would like
to move all the files ending in .f to .F
I tried futilely to do
mv *.f *.F
but it doesn't work.  I am working on a Linux system, but I don't think
that is what is causing the "problem" - I probably just don't have the
right command.   Is there a way to move multiple files like this?   What
command would I have to type?

Thanks for your help.

2. Wallpaper

3. On board Yamaha XG 740

4. Renaming lots of files

5. BeOS

6. newbie need to rename lots of files

7. kernel: isdn_tty: call from # -> # ignored

8. How to rename a lot of files into lower case?

9. Renaming a lot of similarly named files

10. How to rename a lot of files ?

11. Help using awk and bash script to copy and rename lots of index.htm files

12. consistent read() on file while rename() file.new -> file?