Skip to contentSkip to conversation

shell/Perl question

DISCUSSION vick Julius 2 participants 2 messages Nov 3, 2004

lore
Share & tools
lore
#0 lore raw

Thanks Glynn
I tried the first method and It works fine.

What is the use of echo *.jpg in (cd "$dir" && echo *.jpg)? Is it mandatory? Where can I find more details on ${file%%.jpg} stuff? is it in the bash manual?...

thanks

Vick

Quoted 34 lines

From: Glynn Clements <[email protected]>
To: "vick Julius" <[email protected]>
CC: [email protected]
Subject: Re: shell/Perl question
Date: Tue, 2 Nov 2004 06:58:21 +0000

Quoted 2 lines · vick Julius

I have several directories (around 10) named ABC, DEF, GHI,... Each directory contains more than 50 files named, for example, 1.jpg,

2.jpg,

Quoted 2 lines

3.jpg,...50.jpg
These names are the same in each directory (this is what digital cameras

do,

Quoted 5 lines

they give the same names...)
My problem is: I want to put all these files in the same directory, for example, Photos, with the following names:
1ABC.jpg, 2ABC.jpg, ...50ABC.jpg, 1DEF.jpg, 2DEF.jpg...50DEF.jpg,... each image filename (such as 1.jpg) will be split and to insert the name

of

Quoted 1 line

the directory (such as ABC..) after the first part of the name and

finally

Quoted 1 line

append the extension (.jpg) to the filename.

Two examples:

	for dir in ABC DEF GHI ; do
	 for file in `(cd "$dir" && echo *.jpg)` ; do
	  mv "$dir/$file" "Photos/${file%%.jpg}$dir.jpg"
	 done
	done

	find . -type f | sed 's!^\./\(.*\)/\(.*\)\.jpg$!mv \1/\2.jpg 
Quoted 6 lines

Photos/\2\1.jpg!' | sh

If any of the filenames contain spaces, it gets more complex.

--
Glynn Clements <[email protected]>

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! http://search.msn.com/

#1 lore raw
Quoted 8 lines · vick Julius
Quoted 6 lines
	for dir in ABC DEF GHI ; do
	 for file in `(cd "$dir" && echo *.jpg)` ; do
	  mv "$dir/$file" "Photos/${file%%.jpg}$dir.jpg"
	 done
	done

What is the use of echo *.jpg in (cd "$dir" && echo *.jpg)?

That fragment outputs the list of files in the current directory (which will be $dir due to the preceding "cd").

Quoted 1 line

Is it mandatory?

There are other ways to do it; e.g. using "ls *.jpg" would also have worked, but "echo" is built in to the shell.

Quoted 2 lines

Where can I find more details on ${file%%.jpg} stuff? is it in the bash manual?...

It's in the bash manual, in the section entitled "Parameter Expansion".

Signature · 1 line
Glynn Clements <[email protected]>