cancel
Showing results for 
Search instead for 
Did you mean: 

Rename file

SOLVED
Go to solution
la_cool
Occasional Advisor

Rename file

I have a silly question,
I need to rename 10000 files all in 1 shot,
I know "mv command" doesn't support wild card characters

For eg: Files
payee1.dat to payee_plan1.dat
payee2.dat to payee_plan2.dat
payee3.dat to payee_plan3.dat
.....
11 REPLIES
Dave Hutton
Honored Contributor

Re: Rename file

Probably a few different ways of doing this but you can:
filenames=`ls -1 | grep payee`
for filenames in $filenames
do
mv $filenames $filenames.old
done

Dave
Todd McDaniel_1
Honored Contributor

Re: Rename file

Here is something I did once...

Using cut...

for name in `ls *.php3`
do
name1=` echo $name| cut -f 1 -d . `
mv $name1.php3 $name1.php
done


Cut the first part and last part...
Unix, the other white meat.
Todd McDaniel_1
Honored Contributor

Re: Rename file

for name in `ls -la *.dat`
do
name1='echo name|cut 1-5`
name2='echo name|cut 6-10`
mv $name $name1_plan$name2
done

Or make a file and use:

for name in `cat file.in`
Unix, the other white meat.
curt larson_1
Honored Contributor

Re: Rename file

ls payee*.dat |
while read file
do
file=${file%.dat}
file=${file#payee}
mv payee${file}.dat payee_plan${file}.dat
done
Jose Mosquera
Honored Contributor

Re: Rename file

Hi,

SUFIX=payee_plan
FILES=`ls`
for FILE in $FILES
do
DOT_POSSITION=`echo $FILE|awk '{ print index( $1, ".")}'`
POSSITION=`expr $DOT_POSSITION - 1`
SUFIX=`echo $FILE|cut -c$POSSITION-`
NEW_NAME=${PREFIX}${SUFIX}
mv $FILE $NEW_NAME
done

Rgds.
Hein van den Heuvel
Honored Contributor

Re: Rename file

I just yesterday had to do something very similar:

I used this simple (I'm sure I can figure out a shorter 'smarter' solution :-) perl one-liner to do my job. (tested):

ls .*560* | perl -ne 'chop; $o=$_; $_ =~ s/560/585/; rename $o,$_;'


For you that could be (untested):

ls payee*.dat | perl -ne 'chop; $o=$_; $_ =~ s/payee/payee_plan/; rename $o,$_;'


Now, before acting on the files I always make a dry-run with print or echo statements.
So try this first:

ls payee*.dat | perl -ne 'chop; $o=$_; $_ =~ s/payee/payee_plan/; print "rename $o,$_\n";'

hth,

Hein.

harry d brown jr
Honored Contributor

Re: Rename file

perl -e 'open(files2move,"find . -name \"payee*\.dat\" |");while ()
{s/\.\/([a-zA-Z]{1,})([0-9]{1,})\.dat/\/usr\/bin\/mv \1\2.dat \1_plan\2.dat/;print $_;system $_;};'

live free or die
harry
Live Free or Die
John Kittel
Trusted Contributor
Solution

Re: Rename file

I think the best way is creating a custom shell script with vi and executing it directly from within vi.

Start vi without a filename. Read in the list of filenames, one filename per line. Make each line into an mv command. Write the commands to the shell for execution.

example, read in all files in current directory one filename per line. Add a space at the end of each filename. Change each filename to a move command while duplicating the filename. Change the space at the end of each second filename to .new. Write the script to the shell for execution. Exit vi without bothering to save the script.

$vi
$:r !ls -1
:g;$;s;$; ;
:g;.*;s;.*;mv -i &&;g
:g; $;s; $;.new
:w !sh
:q!

Of course many similar operations are possible, limited only by your skill with vi.

John Kittel
Trusted Contributor

Re: Rename file

I forgot to mention, that example renames every file in curent dir to .new

create a sample directory somewhere with several sample files in it and try it out.
curt larson_1
Honored Contributor

Re: Rename file

just another way

ls payee*.dat |
sed 's/\(payee\)\(.*.dat\)/\1\2\
\1_plan\2/' |
xargs -i -t -n2 mv
Highlighted
Muthukumar_5
Honored Contributor

Re: Rename file

Hai,

We can do it with a simple script.

ext=".dat"
name="payee"
new="payee_plan"
i=1

while [[ $i -lt 10001 ]]; do
mv $name$i$ext $new$i$ext
let i=i+1
done

Regards,
Muthukumar
Easy to suggest when don't know about the problem!