Batch rename according to a list

Asus / Eee
April 7, 2009 at 17:45:23
Specs: Linux i686
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


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:


for a in $(ls);
  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"



All help hugely appreciated.

See More: Batch rename according to a list

Report •

April 7, 2009 at 17:48:26
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.

Report •

April 7, 2009 at 18:54:05
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

Report •

April 8, 2009 at 08:34:41
Tried the double quotes too!

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




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.

Report •

Related Solutions

April 8, 2009 at 17:55:39
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

Report •

April 14, 2009 at 03:40:12
Here's how I did it. The tricky part was escaping the variables with the sed script.


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);

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.

Report •

April 14, 2009 at 05:23:32
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

Report •

Ask Question