• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 34181
  • Last Modified:

rename multiple files

I need to rename files from
oct01
oct02
oct03
...
oct31

to
nov01
nov02
nov03
...
nov31

how do I do this?
0
longneckgoosie
Asked:
longneckgoosie
3 Solutions
 
brettmjohnsonCommented:
#!/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
 
avizitCommented:
#!/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
 
brettmjohnsonCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
avizitCommented:
I think oct[0-3][0-9]   will also match oct39  if ever there is such a file
0
 
brettmjohnsonCommented:
> 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
 
avizitCommented:
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
 
ahoffmannCommented:
either use mmv
  mmv 'oct*' 'nov#1'
or perl
  ls oct*|perl -ne 'm/(oct(.*))/;rename$1,"nov$2"'
0
 
byttaCommented:
#! /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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now