Re: Rename file (759 Views)
Reply
Occasional Advisor
la_cool
Posts: 16
Registered: ‎05-20-2004
Message 1 of 12 (759 Views)
Accepted Solution

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
.....
Please use plain text.
Honored Contributor
Dave Hutton
Posts: 579
Registered: ‎11-14-2003
Message 2 of 12 (759 Views)

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
Please use plain text.
Honored Contributor
Todd McDaniel_1
Posts: 1,387
Registered: ‎08-26-2002
Message 3 of 12 (759 Views)

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.
Please use plain text.
Honored Contributor
Todd McDaniel_1
Posts: 1,387
Registered: ‎08-26-2002
Message 4 of 12 (759 Views)

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.
Please use plain text.
Honored Contributor
curt larson_1
Posts: 764
Registered: ‎08-23-2002
Message 5 of 12 (759 Views)

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
Please use plain text.
Honored Contributor
Jose Mosquera
Posts: 1,013
Registered: ‎06-27-2002
Message 6 of 12 (759 Views)

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.
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,585
Registered: ‎05-19-2003
Message 7 of 12 (759 Views)

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.

Please use plain text.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 8 of 12 (759 Views)

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
Please use plain text.
Trusted Contributor
John Kittel
Posts: 402
Registered: ‎07-14-2003
Message 9 of 12 (759 Views)

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.

Please use plain text.
Trusted Contributor
John Kittel
Posts: 402
Registered: ‎07-14-2003
Message 10 of 12 (759 Views)

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.
Please use plain text.
Honored Contributor
curt larson_1
Posts: 764
Registered: ‎08-23-2002
Message 11 of 12 (759 Views)

Re: Rename file

just another way

ls payee*.dat |
sed 's/\(payee\)\(.*.dat\)/\1\2\
\1_plan\2/' |
xargs -i -t -n2 mv
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 12 of 12 (759 Views)

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!
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation