[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

rename multiple files

Posted on 2004-10-29
10
Medium Priority
?
34,178 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
Comment
Question by:longneckgoosie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 172 total points
ID: 12447547
#!/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
avizit earned 164 total points
ID: 12450126
#!/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
ID: 12450182
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:avizit
ID: 12450223
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
ID: 12450267
> 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
ID: 12450316
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
ahoffmann earned 164 total points
ID: 12451657
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
ID: 12485567
#! /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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

656 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