Computing.Net > Forums > Programming > Batch rename according to a list

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Batch rename according to a list

Reply to Message Icon

Name: eointierney
Date: April 7, 2009 at 17:45:23 Pacific
OS: Linux i686
Product: Asus / Eee
Subcategory: Batch
Comment:

Dear all,

I'm trying to rename a bunch of photos according to their subjects' names.

I have several thousand photos of people, each named along the following lines

423big234.jpg

where the 423 is the key to the person's name. I have a list of the correspondence between numbers and names and wish to rename accordingly.

Here's what I have so far:

#!/bin/bash

for a in $(ls);
do
  i=$(awk '{print $1}' $a)

  j=$(awk '{print $2$3$4}' $a)

   for k in $(ls ../femalemodels); do

    target=$(echo $k | sed  's/^$i/$j/g')

    mv "../femalemodels/$k" "../femalemodels/$target"

  done

done

All help hugely appreciated.



Sponsored Link
Ads by Google

Response Number 1
Name: eointierney
Date: April 7, 2009 at 17:48:26 Pacific
Reply:

Sorry, further explanation needed.

I split the list of numbers and names in to separate files, hence the for i in $(ls).

The problem is that the sed isn't working. It keeps telling me the files are the same.


0

Response Number 2
Name: ghostdog
Date: April 7, 2009 at 18:54:05 Pacific
Reply:

try to use double quotes. if that fails, describe clearly again what your files are like, and what you want them to be renamed to. show any input files if there are any

Unix Win32 tools | Gawk for Windows


0

Response Number 3
Name: eointierney
Date: April 8, 2009 at 08:34:41 Pacific
Reply:

Tried the double quotes too!

The files are all jpegs, all named (number)big(number).jpg, and I want to change them from

(number)big(number).jpg

to

(name)big(number).jpg

where (number) is the first field in a text file and (name) is the concatenation of the next three fields.

Here's what I'm trying now:

mv ../femalemodels/$i*.jpg ../femalemodels/new/$j*/

but the wildcard isn't working here either.

I feel so stooopid.


0

Response Number 4
Name: ghostdog
Date: April 8, 2009 at 17:55:39 Pacific
Reply:

how does the files in femalemodels directory look like? you mentioned names, what kind of names? give as much info as possible.

Unix Win32 tools | Gawk for Windows


0

Response Number 5
Name: eointierney
Date: April 14, 2009 at 03:40:12 Pacific
Reply:

Here's how I did it. The tricky part was escaping the variables with the sed script.

listoffiles=~/Desktop/test/target/lists/
files=~/Desktop/test/target/images/
new=~/Desktop/test/target/new/

for a in $(ls $listoffiles); do
	i=$(awk '{print $1}' $listoffiles$a)
	j=$(awk '{print $2$3$4}' $listoffiles$a)
	printf "$i $j\n"

	for k in $(ls $files); do
		echo $k | sed  s/^"$i"/"$j"/g >> out.txt
		cp $files$k $new$(echo $k | sed  s/^"$i"/"$j"/g);
	done
	
done

I had been trying sed 's/^"$i"/"$j"/g' and variations thereon. Silly me.

Anyway it's almost a useful tool now, I'll generalise it so if anyone's interested I'll email it to them.


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: April 14, 2009 at 05:23:32 Pacific
Reply:

do away with the ls in the for loop. its useless. just use shell expansion

for a in $listoffiles

also, try to give your variables meaningful names.

Unix Win32 tools | Gawk for Windows


0

Sponsored Link
Ads by Google
Reply to Message Icon

Problem while executing s... Ok PLEASE Help!!



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch rename according to a list

Random Numbers according to a pdf www.computing.net/answers/programming/random-numbers-according-to-a-pdf/20201.html

Batch Rename files in a folder www.computing.net/answers/programming/batch-rename-files-in-a-folder/15096.html

A problem with batch renaming www.computing.net/answers/programming/a-problem-with-batch-renaming/14963.html