one for the linux Bods..
Author
Discussion

Graham

Original Poster:

16,381 posts

314 months

Thursday 11th May 2006
quotequote all
I need to be able to rename a batch of files either on the contents of the first line of each file or using a list of file names in a separate file


i.e.


single file example, where i want to rename the file with the contents in the first line..

original file name =testfile.txt

contents of the file =
thisisthenrewfilename.txt
line1
line 2
line 3
line 4


for the second possibility i would just have a number or original files i.e text1.txt,text2.txt,text3.txt

and another file with the new file names
i.e. file contects =

newname1.txt
newname2.txt
newname3.txt

or a separated list i.e.

newname1.txt,newname2,txt.newname3.txt.....


or possibly

text1.txt,newname1.txt
text2.txt,newname2.txt
test3.txt,newname3.txt
....

zumbruk

7,848 posts

290 months

Thursday 11th May 2006
quotequote all
First example;

mv testfile.dat `head -1 testfile.dat`

(Note those are backticks, not quotes)

Second example in a minute ...

zumbruk

7,848 posts

290 months

Thursday 11th May 2006
quotequote all
Graham said:

for the second possibility i would just have a number or original files i.e text1.txt,text2.txt,text3.txt

and another file with the new file names
i.e. file contects =

newname1.txt
newname2.txt
newname3.txt

or a separated list i.e.

newname1.txt,newname2,txt.newname3.txt.....


or possibly


text1.txt,newname1.txt
text2.txt,newname2.txt
test3.txt,newname3.txt
....


Personally, if I have to do anything non-trivial with file renaming, I use the perl "rename" script;

www.evolt.org/article/Renaming_Files_with_Perl/17/351/


TheExcession

11,669 posts

280 months

Thursday 11th May 2006
quotequote all
zumbruk said:
(Note those are backticks, not quotes)

Gotta jus luv Linux back ticks...

Graham

Original Poster:

16,381 posts

314 months

Thursday 11th May 2006
quotequote all
hi i need to be able to automate it ..

whats happening is im running an sql report that produces an xml file.

this xml file is actually multiple xml files ( one for each employee)

I then split the file into multiple files using csplit

which gives me

JOB00
JOB01 etc

at the same time i run a 2nd report that outputs a file with what i need as the filenames...

i.e

Prefixjob_number_phone number.xml
i.e
JOBRC01-01234_07970000000

now these 2 lists are in the same order so i can rename the JOBxx files using the contents of the names file.. or at least thats the theory, or I can add the filename somewhere in the xml file rename the file and then strip it out....

ultimatley i end up with a number of files with the same prefix and job number but different tel numbers that get sms'd to another app on the moby...

ta

G

Graham

Original Poster:

16,381 posts

314 months

Thursday 11th May 2006
quotequote all
zumbruk said:
First example;

mv testfile.dat `head -1 testfile.dat`

(Note those are backticks, not quotes)

Second example in a minute ...



Actualy you beauty thats done it perfectly.

if i stick it in the end of my script in a for loop it works bonza

i knew there would be a cute way to do it, I kept going off in awk and perl directions but thats nice and simple

cheers

I owe you a

G


zumbruk

7,848 posts

290 months

Thursday 11th May 2006
quotequote all
Here's your second example, in perl;

#!/bin/perl
#

($listfile = shift @ARGV) || die "Usage: xrn listfile [filenames] ";

open LISTFILE,"<$listfile" || die "Cannot open listfile: $listfile $! ";

while ($_ = shift @ARGV) {
$old_name = $_;
$new_name = <LISTFILE>;
chomp $new_name;
rename ($old_name,$new_name) || die "Cannot rename file $old_name $new_name $
! ";
}

exit(0);

Where "listfile" is the file containing the new names and the list of filenames follows, so if your list of filenames is called "filelist" and the files are called out1, out2, out3 and so on, you'd type;

xrn filelist out*

Enjoy.

Z.