How to rename a lot of files ?

How to rename a lot of files ?

Post by Y W Won » Wed, 17 Mar 1999 04:00:00



I would like to rename a lot of files as follow :-

file1.txt      to         file1.doc
file2.txt      to         file2.doc
      "                           "

Anyone can help ?

Y W Wong

 
 
 

How to rename a lot of files ?

Post by Russe » Wed, 17 Mar 1999 04:00:00



+ I would like to rename a lot of files as follow :-
+
+ file1.txt      to         file1.doc
+ file2.txt      to         file2.doc
+       "                           "
certainly..

$ for x in *txt; do mv $x `echo $x | sed s/txt/doc/`; done

Isn't Unix lovely?

r.

 
 
 

How to rename a lot of files ?

Post by Ken Pizzi » Wed, 17 Mar 1999 04:00:00



Quote:>I would like to rename a lot of files as follow :-

>file1.txt      to         file1.doc
>file2.txt      to         file2.doc
>      "                           "

ls -1d *.txt | sed -e 's/.txt$//' -e 's/.*/mv "&.txt" "&.doc"/' | sh

(This won't work if files have "s or a newlines in their names.)

                --Ken Pizzini

 
 
 

How to rename a lot of files ?

Post by Martin Rams » Wed, 17 Mar 1999 04:00:00


On Tue, 16 Mar 1999 17:59:47 +0800,

Quote:> I would like to rename a lot of files as follow :-

> file1.txt      to         file1.doc
> file2.txt      to         file2.doc
>       "                           "

In case you're using the Bash shell, these are some simple functions to
accomplish changes of filename pre- or postfixes:

    # chpre old_prefix_filepattern new_prefix_string files
    function chpre ()
    {
      local old new cmd a file
      old="$1"; new="$2"; shift 2
      if [ $# -eq 0 ]; then set -- "$old"*; fi

      do
        mv "$file" "$new${file#$old}"
      done
    }

    # chpost old_postfix_filepattern new_postfix_string files
    function chpost ()
    {
      local old new cmd a file
      old="$1"; new="$2"; shift 2
      if [ $# -eq 0 ]; then set -- *"$old"; fi

      do
        mv "$file" "${file%$old}$new"
      done
    }

Examples using these functions:

    chpost .txt .doc
    chpost .txt .doc a*.txt
    chpre sql- SQL-
    chpre '' backup. *.c

To gain a little bit more safety, one could change it something like that:

    function chpre ()
    {
      local old new cmd a file
      old="$1"; new="$2"; shift 2
      if [ $# -eq 0 ]; then set -- ${old}*; fi
      for cmd in 'echo' ''
      do
        if [ "$cmd" = '' ]; then
          echo -n "Really move these files? [n] "
          read a
          case "$a" in y | yes) ;;
            *) echo "Cancel - nothing has been changed."
               return;
          esac
        fi

        do
          $cmd mv -i "$file" "$new${file#$old}"
        done
      done
    }

If you want do more complex filename changes, just use the power of
sed or perl:

    mv "$file" "$( echo "$file" | perl -pe "s|$old|$new|"; )"

Hope, that helps ...

Regards,
  Martin
--

PGP KeyID=0xE8EF4F75 FiPr=52 44 5E F3 B0 B1 38 26  E4 EC 80 58 7B 31 3A D7

 
 
 

How to rename a lot of files ?

Post by Christopher J. Matter » Wed, 17 Mar 1999 04:00:00




> + I would like to rename a lot of files as follow :-
> +
> + file1.txt      to         file1.doc
> + file2.txt      to         file2.doc
> +       "                           "
> certainly..
> $ for x in *txt; do mv $x `echo $x | sed s/txt/doc/`; done
> Isn't Unix lovely?

Changes *all* txt to doc.  Better:

for x in *.txt; do mv $x `echo $x | sed 's/\.txt$/\.doc/'`; done

This means that "txtstuff.txt" becomes "txtstuff.doc", not "docstuff.doc"

                           Chris Mattern

 
 
 

How to rename a lot of files ?

Post by Carlos J. G. Duart » Wed, 17 Mar 1999 04:00:00



> I would like to rename a lot of files as follow :-

> file1.txt      to         file1.doc
> file2.txt      to         file2.doc
>       "                           "

> Anyone can help ?

> Y W Wong

you can use sed, for instance, to "massage" your input text into
commands,
then feed them to sh:

\ls *.txt | sed 's,\(.*\)\.txt$,mv & \1.doc,' | sh

--
carlos

 
 
 

How to rename a lot of files ?

Post by Bo Siltber » Wed, 17 Mar 1999 04:00:00



> I would like to rename a lot of files as follow :-

> file1.txt      to         file1.doc
> file2.txt      to         file2.doc
>       "                           "

> Anyone can help ?

> Y W Wong

If you don't like to play around with various sed and
ls command, you can use this bourne shell script below.
Let's call it 'rename', then you do:

rename *.txt =.doc

It will ask for confirmation.

#!/bin/sh -fh
#
# Rename parts of file names.
#
# Created:  97-10-25 BY Bo Siltberg UAB/K/T             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

outdir=`dirname $output`
output=`basename $output`

# 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 $outdir/$renameto ]; then
    exists=" [ALREADY EXISTS]"
  else
    exists=""
  fi

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

  outputfiles="$outputfiles $outdir/$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

 
 
 

How to rename a lot of files ?

Post by Dale Hagglun » Wed, 17 Mar 1999 04:00:00



Quote:> I would like to rename a lot of files as follow :-

> file1.txt      to         file1.doc
> file2.txt      to         file2.doc
>       "                           "
> Anyone can help ?

I like this perl script.  I know this is comp.unix.shell, but this is
a problem that perl deals with quite a bit more concisely, I think.

------------------------------ cut here ------------------------------
#!/usr/local/bin/perl
$script = shift;


        $old = $_;
        eval $script;

        next if $old eq $_;
        rename $old, $_;

Quote:}

exit 0;
------------------------------ cut here ------------------------------

Run it as


The first argument can be any bit of legal perl code that modifies $_.
Each following argument is assigned in turn to $_, and if the script
modifies it, the file is renamed to the new value.  So, even


would work.  Files that don't end in .txt won't be renamed.

If you're not familiar with perl regexps, you may not want to use this.

Dale.

 
 
 

How to rename a lot of files ?

Post by Tobias Bro » Thu, 18 Mar 1999 04:00:00



> $ for x in *txt; do mv $x `echo $x | sed s/txt/doc/`; done

> Isn't Unix lovely?

Certainly, it's incredible how powerful a good shell combined with
command line utilities is, compared to a rotten GUI. But exactly in
this case I actually think I preffer the 4dos/4os2 'mv' command that
understands what "mv *.txt *.doc" is about. Of course, this won't work
in a shell, as it's the shell and not the command that's expanding the
wildcards. Well, it should of course be possible to write some
alias/utility that understands "rename *.txt '*.doc'"...

--
TobiX                   In a world without fences, who needs gates?
http://www.td.org.uit.no/~tobias/

 
 
 

How to rename a lot of files ?

Post by John Doher » Thu, 18 Mar 1999 04:00:00




> > I would like to rename a lot of files as follow :-

> > file1.txt      to         file1.doc
> > file2.txt      to         file2.doc
> >       "                           "

> > Anyone can help ?

> I like this perl script.  I know this is comp.unix.shell, but this is
> a problem that perl deals with quite a bit more concisely, I think.

> ------------------------------ cut here ------------------------------
> #!/usr/local/bin/perl
> $script = shift;


>         $old = $_;
>         eval $script;

>         next if $old eq $_;
>         rename $old, $_;
> }

> exit 0;
> ------------------------------ cut here ------------------------------

I wouldn't call that real concise, especially when compared to

  example $ for i in *.txt ; do mv $i ${i%%.txt}.doc ; done

Ain't ksh great?

 
 
 

How to rename a lot of files ?

Post by John DuBo » Fri, 19 Mar 1999 04:00:00




Quote:>Of course, this won't work
>in a shell, as it's the shell and not the command that's expanding the
>wildcards. Well, it should of course be possible to write some
>alias/utility that understands "rename *.txt '*.doc'"...

I believe that in zsh, you can instruct the shell not to perform filename
globbing on the arguments to certain commands.  So if you add rename to this
list, you don't even need to quote the arguments.  A suitable rename utility
is at ftp://ftp.armory.com./pub/scripts/rename

        John
--

 
 
 

How to rename a lot of files ?

Post by Fredrik Roube » Fri, 19 Mar 1999 04:00:00




Quote:>> $ for x in *txt; do mv $x `echo $x | sed s/txt/doc/`; done

>> Isn't Unix lovely?

> Changes *all* txt to doc.  Better:

> for x in *.txt; do mv $x `echo $x | sed 's/\.txt$/\.doc/'`; done

> This means that "txtstuff.txt" becomes "txtstuff.doc", not "docstuff.doc"

Actually the original solution would rename "txtstuff.txt" to
"docstuff.txt" (because the option 'g' was not present to sed).

An easier way to do it:

for x in *.txt ; do mv $x ${x%%.txt}.doc ; done

It is also faster as you don't have to spawn a new process each iteration.

H?lsningar // Fredrik Roubert

--


http://www.efd.lth.se/~d95fr/         SE-222 40 Lund

 
 
 

How to rename a lot of files ?

Post by Allan Hawdo » Sat, 20 Mar 1999 04:00:00



: + I would like to rename a lot of files as follow :-
: +
: + file1.txt      to         file1.doc
: + file2.txt      to         file2.doc
: +       "                           "
: certainly..

: $ for x in *txt; do mv $x `echo $x | sed s/txt/doc/`; done

: Isn't Unix lovely?

Hee. And you thought DCL was verbose:

        ren file*.txt .doc

:-)

 
 
 

How to rename a lot of files ?

Post by David Lod » Sun, 21 Mar 1999 04:00:00


On 17 Mar 1999 21:00:00 +0100, Tobias Brox



>> $ for x in *txt; do mv $x `echo $x | sed s/txt/doc/`; done

Or the quicker way; using just ksh family builtins (except for mv):

for i in *.txt; do mv $i ${$i%.txt}.doc; done

Who sayes you can't make shell efficient?

dave
--

"Dear God above (if you exist), Hope you see the funny side to this,
 Now don't get cross - don't bite your nails,
 Oh, Son of Man your mission's failed."
 - Skyclad "The Sinful Ensemble"

 
 
 

How to rename a lot of files ?

Post by Al A » Mon, 22 Mar 1999 04:00:00


        this is where dos out-terses unix:
                ren *.txt *.doc
--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, seders moderator                                      sed u soon
               it is not zat we do not see the  s o l u t i o n          
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

 
 
 

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. Crystal Semiconductor?

3. computer-security/compromise FAQ

4. Renaming lots of files

5. "rootlogging" under Solaris?

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

7. Bizarre mouse problem when downloading

8. newbie need to rename lots of files

9. Renaming a lot of similarly named files

10. Renaming lots 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?