How to define two variable in foreach command??

Hello,

I just want to know how If it's possiple to define 2 variable using foreach command ???

I have directory inside that directory around 1000 file, I want to rename all of this files to something I have it in a list. Example :-

------This is what in my directory----------
d1
d2
d3
d4
d5
------------------------------------------------------

I have a LIST which containe the new naming "more new_name" and I have this

omd1
omd2
omd3
omd4
omd5

I tried to do crazy way to done it :-

# ls mydirectory >> old_name
# foreach file (`more old_name`) file2 (`more new_name`)
mv $file $file2
end

I know this is crazy but I don't know how to do it :smiley:

Please do help me :frowning:

hi,
i would suggest a different approach.

list the names of all the files in a file

$ ls > outfile

now the file new_name is expected to contain 1000 names
so paste the two files

$paste outfile new_name > newfile

the newfile will contain

d1 omd1
d2 omd2
d3 omd3
... and so on

now open the file and prepend "mv" to the beginning of each line

$vi newfile

:%s/^/mv /g

don't forget the space
now save the file
and say
$sh newfile

and bingo...

hope it helps

asif

Asif,

:%s/^/mv /g

Here can u explain what %s signifies.. ^ denotes the beginning.. and what is this g for?? it denotes all the lines is it??

TIA,
Nisha

:%s/^/mv /g

this is a command mode operation of the vi editor.

open any file in the vi editor
press ESC once and type :

you will get this prompt on the bottom left of your screen

%s is for substitution.
^ is a special symbol which denotes the beginning of each line
"mv " please note there is a space after mv is the string to be substituted
and g is for replacing all the instances
g is of no use here as there will be only one beginning of the sentence in each line, but if there are multiple instances of the search string ( in this case "^"), the option g is used to replace all the instances. if not given, then only the first instance of the search string in each line will be replaced.

asif

sorry
i wrote more than what you asked.
didn't read your mail carefully

well from what your example says why dont you just do it this way.

#! /bin/ksh
cd (path to your directory here)
for i in `ls`; do
mv $i om$i
done

but then again. maby im not understanding your question.

%s is substitution across all rows in the file.

You could use '1,$ s' for the same (1 is first $ is last) - which is the same - but you also can do '1,10 s' which is rows 1 to 10.....etc.

Thanks Peter..I got that...

nisha, your solution is fine. but what if the filenames are not in order

sort the files...

no
i mean to say
what if the names are not d1,d2,d3...
and something like

myfile, usr,bin,web,index ... etc.

The use the paste solution suggested earlier.

file_1 file_2 > file_3
------ ------- ------
111 cat 111 cat
222 dog 222 dog
333 cow 333 cow

paste -d" " file_1 file_2 > file_3
sed -e "s/^/mv /g" ie "s/$/;/g" file_3 > file_4

The chmod 755 for file_4 and run it. You'll have a log of the mappings in file 3 and 4 as well.

file_4
-------
mv 111 cat;
mv 222 dog;
mv 333 cow;

But still wont the sort help ... it sorts in the alphabetical order and then we can proceed using the paste command and so on...

what do you want to sort.
when the filenames are random, then you can not generate those filenames using this

secondly, when you are using the paste command, there is no need to sort as the list could be required in any order depending on the the user.

Yeah...the sorting is dependant on what is wanted to be acheived.

The 'ls' command will produce a sorted list of files in a directory.
The file containing the new names may or may not require sorting - depends on the users need. Sort may well be needed and used. But it may not too!