renaming files from an array of names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting renaming files from an array of names
# 1  
Old 08-14-2010
renaming files from an array of names

I haven’t used Unix in over 25 years … and so I am at a loss for something that should be very simple. I have a lot of jpeg files (i.jpg) of students in a yearbook.. I also have an array name(i) of their names. I need to rename each “i.jpg” to “name(i).jpg”. I believe the ksh script should be of the nature of

Code:
  for i = 1 to n
  do 
              mv ”$i.jpg”  “name($i).jpg”
  done

but I don’t remember how to make the name(i) array visible to the script. Any help would be greatly appreciated.

Chuck

Last edited by Franklin52; 08-15-2010 at 08:18 AM.. Reason: Please use code tags
# 2  
Old 08-14-2010
Code:
ARR=( a b c d e f g h i j )

for ((N=0; N<10; N++))
do
        mv "${N}" "${ARR[$N]}.jpg"
done

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-14-2010
Many thanks for the quick response.

I misstated: the names are not in an array but in a file: names(i) 0<i<700

Chuck
# 4  
Old 08-15-2010
I'll make a couple of assumptions about the file of names; you should be able to modify the script below based on what the file's contents really are.

Code:
#!/usr/bin/env ksh
# assume name_file contains two, blank separated fields, on each line
# first is index number that corresponds i.jpg, and second is the name
cat name_file | while read index name
do
     mv $index.jpg $name.jpg
done

I sometimes run a test using echo rather than mv to see what might happen before actually renaming things. That has saved my butt from disaster, or a nasty bit of work to recover the damage, on several occasions.

Hope this was close to what you need.
This User Gave Thanks to agama For This Post:
# 5  
Old 08-15-2010
Agama,

Not home now so can't complete the task, but that is the solution. Also appreciate the wisdom of using echo. Many thanks!

Chuck
# 6  
Old 08-15-2010
Quote:
Originally Posted by chuckmg
Many thanks for the quick response.

I misstated: the names are not in an array but in a file: names(i) 0<i<700

Chuck
Code:
for file in $(cat name_file)
do
  mv $file name${file}
done

or

Code:
for i in seq {1..700}
do
  mv $i.jpg name$i.jpg
done

# 7  
Old 08-15-2010
Solution in sh
Assuming:
- Files:
Code:
unix.com$ ls *jpg
1.jpg  2.jpg  3.jpg  4.jpg

-Data file (fields are separated with TAB):
Code:
unix.com$ cat f1
1    First name
2    Second name
3    Third name
4    Fourth Name

This sh code:
Code:
#!/bin/sh

while read l; do
    id=$(echo "$l" | cut -f1)
    name=$(echo "$l" | cut -f2)
    mv "$id".jpg "$name".jpg    
done < f1

exit 0

gives
Code:
unix.com$ ls *jpg
First name.jpg    Fourth Name.jpg  Second name.jpg  Third name.jpg

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Renaming the file names in a directory

Hi, I have about 60 files in a directory and need to rename those files. For example the file names are i_can_phone_yymmdd.txt (where yymmdd is the date. i.e 170420 etc) i_usa_phone_1_yymmdd.txt i_eng_phone_4_yymmdd.txt The new file names should be phone.txt phone_1.txt phone_4.txt I am... (4 Replies)
Discussion started by: naveed
4 Replies

2. Shell Programming and Scripting

Renaming File Names in a folder/Dir

Hi Team, I'm new to Unix shell scripting . I've the following requirement A folder contains the list of files with the following format ab.name.11.first ab.name.12.second ab.name.13.third ---------- I have to rename the above file to like below ... (6 Replies)
Discussion started by: smile689
6 Replies

3. UNIX for Dummies Questions & Answers

Renaming files with weird names

I have hundreds of files with weird names, something like this: I was wondering how can I rename them all keeping the sampleid and the last extension, something like this: Any help will be greatly appreciated. (5 Replies)
Discussion started by: Xterra
5 Replies

4. Shell Programming and Scripting

Shell Scripts (Renaming file names with sequential numbers)

Hi there, Firstly, I have no experience with shell scripts so would really appreciate some help. I have the following shell script that is causing some problems: moveit() { && set -x if then DOUBLE_DELIVERY=$(grep... (6 Replies)
Discussion started by: thebeno
6 Replies

5. Shell Programming and Scripting

How to store files names from a directory to an array

Hi I want to store the file names into an array. I have written like this but I am getting error. declare -A arr_Filenames ls -l *.log | set -A arr_Filenames $(awk '{print $9}') index=0 while (( $index < ${#arr_Filenames })); do Current_Filename=${arr_Filenames} ... (5 Replies)
Discussion started by: dgmm
5 Replies

6. UNIX for Dummies Questions & Answers

Renaming files in one file from names in other

Hi Guys, I have a small problem of renaming multiple files. For example I have names of a set of files in one directory like K2_34625-34675 K7_988963-988983 K12_773882-7734102 and the other set corresponding to the same is U_P_321_9_3_11.ab1 U_P_322_9_3_11.ab1 U_P_323_9_3_11.ab1 Now... (23 Replies)
Discussion started by: pawannoel
23 Replies

7. Shell Programming and Scripting

Renaming file names

I have 7 files with 7 different names coming into a specified folder on weekly basis, i need to pick a file one after another and load into oracle table using sql loader. I am using ksh to do this. So in the process if the file has error records and if sql loader fails to load into oracle tables,... (2 Replies)
Discussion started by: vpv0002
2 Replies

8. UNIX for Dummies Questions & Answers

Some questions - renaming duplicate names

I have a file that looks like this 2 4 10 500 tim9 5 8 14 700 tim9 3 5 15 432 john1 1 4 12 999 ellen2 So basically what i want to do is fine duplicate names on column 5 and rename it with an extention (i.e. tim9_1 and tim9_2). so the output file will look like this 2 4 10 500 tim9_1... (1 Reply)
Discussion started by: kylle345
1 Replies

9. Shell Programming and Scripting

Renaming file names in a shell script

I want to write a shell script that will rename all the file names to today's date attached to it.. so for example i have a file names like file1.sales.20081201.txt.c zbrs.salestxtn.20091101.txt.inn then it will rename both the files with todays date to it so the file names get changed... (1 Reply)
Discussion started by: rudoraj
1 Replies

10. Shell Programming and Scripting

Variable names within array call

I am trying to write a piece of code that will call a value from an array. There are multiple arrays that I need to call data from. Only one array needs to be used based on the step within the program. The arrays have the names "cue_0", "cue_1", and so on. I can't figure out how to call a value... (2 Replies)
Discussion started by: vockleya
2 Replies
Login or Register to Ask a Question