Solved

rename multiple files

Posted on 2004-10-29
34,121 Views
Last Modified: 2011-08-18
I need to rename files from
oct01
oct02
oct03
...
oct31

to
nov01
nov02
nov03
...
nov31

how do I do this?
0
Question by:longneckgoosie
    8 Comments
     
    LVL 23

    Accepted Solution

    by:
    #!/bin/bash
    # This Bash shell script emulates the linux 'rename (1)' command
    # The "usage" man page help  was shamelessly stolen from Linux.
    # 04 Aug 2004  Brett Johnson  

    # Must have at least three args: fromtext, totext, file...
    if [ $# -lt 3 ] ; then
      cat <<END
    NAME
            rename - bulk rename files

    SYNOPSIS
            rename fromtext totext files...

    DESCRIPTION
            rename will rename the specified files by replacing the
            first occurrence of fromtext in their name with totext.

            For example, given the files foo1, ..., foo9, foo10, ..., foo278,
            the commands

              rename foo foo0 foo?
              rename foo foo0 foo??

            will turn them into foo001, ..., foo009, foo010, ..., foo278.

            And

              rename .htm .html *.htm

            will fix the extension of your html files.

    END

    else
      fromtext=$1
      totext=$2
      shift 2

      for f in "$@" ; do mv $f ${f/$fromtext/$totext} ; done
    fi
    0
     
    LVL 11

    Assisted Solution

    by:avizit
    #!/bin/bash
    for fil in `ls oct[0-9]*`
    do
      echo $fil
      new=`echo $fil | sed  -e 's/oct/nov/g'`
      mv $fil $new
    done


    The above will do the same thing , but the downside being that it will also rename files  like oct32, oct44 ,,,,oct 999
    so ifyou can gurantee that such files do not exist than this will work for you :)
    0
     
    LVL 23

    Expert Comment

    by:brettmjohnson
    The script I provided was generic.  Use it as follows to solve your problem:

    /path/to/rename oct nov oct[0-3][0-9]


    0
     
    LVL 11

    Expert Comment

    by:avizit
    I think oct[0-3][0-9]   will also match oct39  if ever there is such a file
    0
     
    LVL 23

    Expert Comment

    by:brettmjohnson
    > I think oct[0-3][0-9]   will also match oct39  if ever there is such a file

    That is much less likely a problem than renaming oct31 to nov31  ;)

    0
     
    LVL 11

    Expert Comment

    by:avizit
    heh yeah.. I was thinking bout Feb .. dint strike me that the same problem can occur in almost every consecutive month save jul-aug
    0
     
    LVL 51

    Assisted Solution

    by:ahoffmann
    either use mmv
      mmv 'oct*' 'nov#1'
    or perl
      ls oct*|perl -ne 'm/(oct(.*))/;rename$1,"nov$2"'
    0
     
    LVL 4

    Expert Comment

    by:bytta
    #! /bin/bash
    # just the days in your month...
    for i in $( cal | tail +3);
    do
    [ i -lt 10 ] && i=0$i;# zero padding
    mv oct$i nov$i;
    done
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
    Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now