Renaming lots of files

Renaming lots of files

Post by Ray Wo » Sat, 18 Oct 1997 04:00:00



I have a bunch of files like "a.HTM, a.HTM" etc.  I want to rename
them "a.html, b.html".  

So, I tried cp *.HTM *.html.  I get the following message:

cp: Target 201_01.html must be a directory
Usage: cp [-i] [-p] f1 f2
       cp [-i] [-p] f1 ... fn d1
       cp [-i] [-p] [-r] d1 d2

(201_01.html is a file in the directory).

So, what do I have to do to rename these files?

Thanks,

Ray

 
 
 

Renaming lots of files

Post by Bryan C. Andre » Sat, 18 Oct 1997 04:00:00



Quote:> I have a bunch of files like "a.HTM, a.HTM" etc.  I want to rename
> them "a.html, b.html".  

> So, I tried cp *.HTM *.html.  I get the following message:

You cannot do this, the second wildcard makes no sense to the shell.

% for in *.HTM; do

Quote:>    mv $i ${i%%HTM}html
> done

--

"Donnie were much more 'user-friendly'. May be you selective
       about friends:-)" -- Levente Farkas

"Hey, wait a minute, you clowns are on dope!"
        -- Owen Cheese in 'Shakes the Clown'

 
 
 

Renaming lots of files

Post by Ralph Finc » Sat, 18 Oct 1997 04:00:00


There is also something called mmv; no doubt a search engine can find
it.

--
Ralph Finch, Senior Engineer, W.R.      Tel.    916-653-8268
Delta Modeling Section                  Fax.    916-653-6077

1416 Ninth St, Sacramento, CA 95814     finger for PGP public key

 
 
 

Renaming lots of files

Post by Brian Salter-Du » Sun, 19 Oct 1997 04:00:00




>> I have a bunch of files like "a.HTM, a.HTM" etc.  I want to rename
>> them "a.html, b.html".  

>> So, I tried cp *.HTM *.html.  I get the following message:

>You cannot do this, the second wildcard makes no sense to the shell.

>% for in *.HTM; do
>>        mv $i ${i%%HTM}html
>> done

I use a routine called mved ( there is also one called cped).
The useage is

mved =.HTM =.HTML

Unfortunately I can not recall where it came from. Can anyone help
and give the source.

Cheers, Brian.
--
        Associate Professor Brian Salter-Duke (Brian Duke)
School of Mathematical and Physical Sciences, Northern Territory University,
  Darwin, NT 0909, Australia.  Phone 08-89466702. Fax 08-89466847

 
 
 

Renaming lots of files

Post by Thomas K?hle » Sun, 19 Oct 1997 04:00:00




Quote:> I have a bunch of files like "a.HTM, a.HTM" etc.  I want to rename
> them "a.html, b.html".  
> So, I tried cp *.HTM *.html.  I get the following message:
> cp: Target 201_01.html must be a directory
> Usage: cp [-i] [-p] f1 f2
>        cp [-i] [-p] f1 ... fn d1
>        cp [-i] [-p] [-r] d1 d2
> (201_01.html is a file in the directory).
> So, what do I have to do to rename these files?

Using sh, you could do something like

for i in *.HTM
do
cp $i `basename $i .HTM`.html
done

See basename(1) and cp(1) [and possibly mv(1)] for details.
As you wanted to rename files, you should rather use "mv"
instead of "cp"...

HTH,
     Thomas

--


   IRC: jeanluc  Channels:  #star_trek #linuxger #ixthys #knf
             WWW: http://home.pages.de/~jeanluc/

 
 
 

Renaming lots of files

Post by Robert Ega » Mon, 20 Oct 1997 04:00:00


This command should work

foreach file (*.htm)
   mv $file $file:r.html
   end

HTH
Robert Egan
--
To reply by mail, set nospam ==> regan


> I have a bunch of files like "a.HTM, a.HTM" etc.  I want to rename
> them "a.html, b.html".

> So, I tried cp *.HTM *.html.  I get the following message:

> cp: Target 201_01.html must be a directory
> Usage: cp [-i] [-p] f1 f2
>        cp [-i] [-p] f1 ... fn d1
>        cp [-i] [-p] [-r] d1 d2

> (201_01.html is a file in the directory).

> So, what do I have to do to rename these files?

> Thanks,

> Ray

 
 
 

Renaming lots of files

Post by Pete Houst » Tue, 21 Oct 1997 04:00:00



|I have a bunch of files like "a.HTM, a.HTM" etc.  I want to rename
|them "a.html, b.html".  
|
[munch]
|
|So, what do I have to do to rename these files?

Download the FAQ and read the answer to number 2.6

                        Pete
--

PO Box 220, Whiteknights, Reading, | Phone: +44-118-9875123 ext 7594
Berkshire, RG6 6AF, United Kingdom | Fax:   +44-118-9750203                
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WWW: http://www.rdg.ac.uk/~spr96phh/pete.html Use lynx - you know you want to!

 
 
 

Renaming lots of files

Post by Michael E. Wallac » Wed, 22 Oct 1997 04:00:00


--------------3EC3AAD30C6A076C7500485C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is a classic textbook example of the csh - foreach command.
However, some text was left out of the first line.  The code in
parentheses (sp?) should generate a word list for the variable file
hence the use of backquotes (`).  So the first line with the rest of the
shell should look like this:

foreach file (`ls *.htm`)
   mv $file $file:r.html
   end

This can also be run from the command line:

% foreach file (`ls *.htm`)
    ?  mv $file $file:r.html
    ?  end
%

Michael Wallace
USN


> This command should work

> foreach file (*.htm)
>    mv $file $file:r.html
>    end

> HTH
> Robert Egan
> --
> To reply by mail, set nospam ==> regan


> > I have a bunch of files like "a.HTM, a.HTM" etc.  I want to rename
> > them "a.html, b.html".

> > So, I tried cp *.HTM *.html.  I get the following message:

> > cp: Target 201_01.html must be a directory
> > Usage: cp [-i] [-p] f1 f2
> >        cp [-i] [-p] f1 ... fn d1
> >        cp [-i] [-p] [-r] d1 d2

> > (201_01.html is a file in the directory).

> > So, what do I have to do to rename these files?

> > Thanks,

> > Ray

--------------3EC3AAD30C6A076C7500485C
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<HTML>
This is a classic textbook example of the csh - <I>foreach</I> command.&nbsp;
However, some text was left out of the first line.&nbsp; The code in parentheses
(sp?) should generate a word list for the variable <I>file </I>hence the
use of <U>backquotes</U> (<B>`</B>).&nbsp; So the first line with the rest
of the shell should look like this:

<P>foreach file (`ls *.htm`)
<BR>&nbsp;&nbsp; mv $file $<A HREF="file:r.html">file:r.html</A>
<BR>&nbsp;&nbsp; end

<P>This can also be run from the command line:

<P>% foreach file (`ls *.htm`)
<BR>&nbsp;&nbsp;&nbsp; ?&nbsp; mv $file $<A HREF="file:r.html">file:r.html</A>
<BR>&nbsp;&nbsp;&nbsp; ?&nbsp; end
<BR>%

<P>Michael Wallace
<BR>USN

<BLOCKQUOTE TYPE=CITE>This command should work

<P>foreach file (*.htm)
<BR>&nbsp;&nbsp; mv $file $<A HREF="file:r.html">file:r.html</A>
<BR>&nbsp;&nbsp; end

<P>HTH
<BR>Robert Egan
<BR>--
<BR>To reply by mail, set nospam ==> regan

<BR>>
<BR>> I have a bunch of files like "a.HTM, a.HTM" etc.&nbsp; I want to
rename
<BR>> them "a.html, b.html".
<BR>>
<BR>> So, I tried cp *.HTM *.html.&nbsp; I get the following message:
<BR>>
<BR>> cp: Target 201_01.html must be a directory
<BR>> Usage: cp [-i] [-p] f1 f2
<BR>>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cp [-i] [-p] f1 ... fn
d1
<BR>>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cp [-i] [-p] [-r] d1 d2
<BR>>
<BR>> (201_01.html is a file in the directory).
<BR>>
<BR>> So, what do I have to do to rename these files?
<BR>>
<BR>> Thanks,
<BR>>
<BR>> Ray</BLOCKQUOTE>
&nbsp;</HTML>

--------------3EC3AAD30C6A076C7500485C--

 
 
 

Renaming lots of files

Post by Bo Siltber » Fri, 31 Oct 1997 04:00:00



>> > I have a bunch of files like "a.HTM, a.HTM" etc.  I want to rename
>> > them "a.html, b.html".

This general rename script that I just wrote should do it too:

rename *.HTM =.html

Bo Siltberg

#!/bin/sh -fh
#
# Rename parts of file names.
#
# Created:  97-10-25 BY Bo Siltberg             REV. A
#
# Format: rename inputfiles outputfile
#

pgmname=`basename $0`

usage()
{
  more << END_HELP

  Rename a number of files.

  Format: $pgmname infiles... outfile

  The file names are split into units. An "=" in the output unit is
  replaced with the corresponding name from the input unit.

  Example:
  $pgmname mfm.code =R6A.=         -> mfmR6A.code
  $pgmname mfm.code bus.=          -> bus.code
  $pgmname mfm.code ==.=           -> mfmmfm.code
  $pgmname mfm.code.gz bus.=       -> bus.code.gz
  $pgmname mfm.code.gz =.object.=  -> mfm.object.gz

  However, the basic idea with this command is to rename a number of
  files:
  $pgmname mfm.* bus.=             -> All files starting with 'mfm.' are
                                    given the prefix 'bus.' instead.

END_HELP

Quote:}

changefilename()
{
  if [ $# != 2 ]; then     # take input from pipe
    read f1 f2            
  else                     # take input from arguments
    f1=$1
    f2=$2
  fi

  \echo "$f1\n$f2" | \
    nawk '{
      if ( NR == 1 ) {
        n1 = split( $0, ins, ".")
        input = $0
      }
      else {
        n2 = split( $0, out, ".")
        output = $0
      }
    }
    END {
      if ( n1 < n2 ) {
        printf "%s: Output name '"'%s'"' has more units than input name '"'%s'"'.\n",
             program, output, input | "cat >&2"
        exit 2  
      }

      outline = ""
      for ( i = 1; i <= n2; i++ ) {
        gsub("=", ins[i], out[i])
        outline = outline "." out[i]
      }
      for ( i = n2 + 1; i <= n1; i++ ) {
        outline = outline "." ins[i]
      }
      print substr(outline,2)

    }' program=$pgmname

  return $?

Quote:}

if [ $# -lt 2 ] || [ "X$1" = "X-h" ] || [ "X$1" = "X-help" ]; then
  usage
  exit 1
fi

# Last arg is output name
inputfiles=""
while [ $# -gt 0 ]; do  
  n=`echo "$1" | nawk '{ print NF }'`
  if [ $n != 1 ]; then
    echo "Invalid file name: '$1'."
    exit 2
  fi

  if [ $# = 1 ]; then     # Last argument in 'output'
    output=$1
  else
    inputfiles="$inputfiles $1"
  fi
  shift
done

# Go through input names and check that they are ok
outputfiles=""
count=0
for file in $inputfiles; do
  count=`expr "$count" + 1`

  base=`basename $file`
  dir=`dirname $file`
  renameto=`changefilename $base $output`
  rc=$?; if [ $rc != 0 ]; then exit $rc; fi

  if [ -f $dir/$renameto ]; then
    exists=" [ALREADY EXISTS]"
  else
    exists=""
  fi

  echo "Rename: $file -> $dir/$renameto $exists"

  outputfiles="$outputfiles $dir/$renameto"
done

\echo "Confirm rename of $count files Y/N: \c"
stty raw
ans=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty -raw
echo ""
if [ "X$ans" != "Xy" ]; then
  echo "Cancelled."
  exit 1
fi

count=0
for file in $inputfiles; do
  count=`expr "$count" + 1`
  out=`echo "$outputfiles" | nawk '{ print $'$count' }'`
  \mv -i $file $out
  rc=$?; if [ $rc != 0 ]; then exit $rc; fi
done

exit

 
 
 

Renaming lots of files

Post by Christian Bauernfei » Sun, 02 Nov 1997 04:00:00




Quote:> This seems like an awful lot of trouble to go to.  I know in es
> (ftp://ftp.cs.uchicago.edu/pub/users/csdayton) you can do it as simply as

> for (file = *.HTM) {
>   mv $file <={~~ $file *.HTM}.html
> }

(t)csh:

foreach i (*.HTM)
mv $i ${i:r}.html
end

bash:

for i in *.HTM; do
mv $i ${i%HTM}.html
done

ksh: no idea

I'd also have suggested

ls *.HTM | xargs -i mv {} `basename {} HTM`html

but that is broken...

Christian
--
Christian Bauernfeind

 
 
 

Renaming lots of files

Post by Bo Siltber » Wed, 05 Nov 1997 04:00:00






>> >> > I have a bunch of files like "a.HTM, a.HTM" etc.  I want to rename
>> >> > them "a.html, b.html".

>> This general rename script that I just wrote should do it too:

>> rename *.HTM =.html

>> Bo Siltberg

>> <shell script removed>
>This seems like an awful lot of trouble to go to.  I know in es
>(ftp://ftp.cs.uchicago.edu/pub/users/csdayton) you can do it as simply as
>for (file = *.HTM) {
>  mv $file <={~~ $file *.HTM}.html
>}

I've done the "trouble" already, so no trouble for you.
And the result is a simple command syntax of a general purpose
renaming script.

I cannot understand why one would like to write commands like
"mv $file <={~~ $file *.HTM}.html" all the time. Unless one are
an *ed UNIX hacker, it is time consuming to find out
this syntax. But UNIX is maybe not meant for "normal" users?

Bo Siltberg

 
 
 

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. ODB database

3. rsh w/ no password in slackware 7.1.0??

4. newbie need to rename lots of files

5. Need Information re Printer setup

6. Renaming lots of files

7. Timelines

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?