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
# 8  
Old 08-20-2010
This one worked

The suggestion above by Agama is the only one that I could get to give me 'almost there' results. There is still a question but I will start by showing the one that worked:

namefile
Code:
01.jpg Arthur Adams.jpg
02.jpg Catherine Albero.jpg
03.jpg Ronald Alexander.jpg
04.jpg Elizabeth Alfano.jpg
05.jpg Fran Alfano.jpg

Script
Code:
#!/bin/ksh
cat namefile | while read index name
do
    echo $index.jpg $name.jpg
done

Results
Code:
01.jpg Arthur Adams.jpg
02.jpg Catherine Albero.jpg
03.jpg Ronald Alexander.jpg
...

I'll document the problem in next submission

---------- Post updated at 06:57 PM ---------- Previous update was at 06:41 PM ----------

This is a continuation of previous reply. I can not make any sense of the error messages. This script is the same as the previous one but with "cp" instead of "echo".Smilie

Script to rename files:
Code:
#!/bin/ksh
cat namefile| while read index name
do
  cp $index.jpg $name.jpg 
done

Error messages
Code:
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
...


Moderator's Comments:
Mod Comment Please use code tags, thank you!

Last edited by Franklin52; 08-21-2010 at 03:42 PM..
# 9  
Old 08-20-2010
Thanks for posting a sample of your input -- wouldn't have had a clue without it.

Your problem is blanks in the target name. The cp command is seeing three parameters:

Code:
cp name.jpg arthur adams.jpg

My suggestion is to replace the spaces in your input file such that they are arthur.adams.jpg or arthur_adams.jpg, or you can do it on the cp command like this:

Code:
cp $index.jpg ${name// /_}.jpg

The ${name// /_}.jpg causes all blanks to be replaced with an underbar. Be careful as Kshell replacement syntax is not identical to regex (no closing slant) and I don't believe this is supported in Bash.
This User Gave Thanks to agama For This Post:
# 10  
Old 08-21-2010
Many thanks Agama! I assumed that if it worked for echo, as you suggested trying in your first message, it would work for cp. Live and learn!.

Chuck
# 11  
Old 08-21-2010
Quote:
Originally Posted by chuckmg
Many thanks Agama! I assumed that if it worked for echo, as you suggested trying in your first message, it would work for cp. Live and learn!.

Chuck
Glad that worked. The echo lets me see the command that would be executed to ensure that it's not grabbed more things than I was expecting, and to ensure that it's gotten the right list of things. Unfortunately as you discovered, it doesn't point out all issues.
# 12  
Old 08-21-2010
Quote:
Originally Posted by chuckmg

Script to rename files:
#!/bin/ksh
cat namefile| while read index name
do
cp $index.jpg $name.jpg
done
Code:
#!/bin/ksh
while read index name
do
cp "$index.jpg" "$name.jpg"
done < namefile

Use double quote(best practice) and remove the file extension.

Code:
#!/bin/ksh
while read index name
do
cp "$index" "$name"
done < namefile

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