bash script help

0 views
Skip to first unread message

Peter Martin

unread,
Jun 18, 2010, 12:20:32 AM6/18/10
to kplug
I've converted a bunch of slides to my computer. The problem I'm having is the script is not processing the first file ( ... 005.jpg). Many will have the same file name but they are in different directories. Here's a sample:

/mnt/store01/test
peter@pm01: $ find . -name '*.[jJ]*'
./01/01_1968-1969_ElkhartIN/5-27-2010_005.jpg
./01/01_1968-1969_ElkhartIN/5-27-2010_009.jpg
./01/01_1968-1969_ElkhartIN/5-27-2010_001.jpg
./01/01_1968-1969_ElkhartIN/5-27-2010_003.jpg
./01/01_1968-1969_ElkhartIN/5-27-2010_008.jpg
./01/01_1968-1969_ElkhartIN/5-27-2010_006.jpg
./01/01_1968-1969_ElkhartIN/5-27-2010_004.jpg
./01/01_1968-1969_ElkhartIN/5-27-2010_007.jpg
./01/01_1968-1969_ElkhartIN/5-27-2010_002.jpg

The goal is to rename the jpg's to prepend the main dir name so the last one will look like:
./01/01_1968-1969_ElkhartIN/01_5-27-2010_002.jpg

This is the bash script:
#!/bin/bash
find . -name '*.[jJ]*' | while read f
do
   # change file name to pre-pend the main dir name
  
   echo $f
   #awk -F/ '{file=sprintf("%s%s%s",$2,"_",$4); print file; sub($4, file); sub("JPG", "jpg"); print $0}'
  
done

With the echo I get the list as shown at the top, but when I execute the awk script I get this:
peter@pm01: $ ./FileNameChange.sh
01_5-27-2010_009.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_009.jpg
01_5-27-2010_001.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_001.jpg
01_5-27-2010_003.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_003.jpg
01_5-27-2010_008.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_008.jpg
01_5-27-2010_006.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_006.jpg
01_5-27-2010_004.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_004.jpg
01_5-27-2010_007.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_007.jpg
01_5-27-2010_002.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_002.jpg

You'll note the missing 005.jpg.  That's what I'm trying to figure out. Why is it missing? With both the echo and awk I get:
./01/01_1968-1969_ElkhartIN/5-27-2010_005.jpg
01_5-27-2010_009.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_009.jpg
01_5-27-2010_001.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_001.jpg
01_5-27-2010_003.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_003.jpg
01_5-27-2010_008.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_008.jpg
01_5-27-2010_006.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_006.jpg
01_5-27-2010_004.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_004.jpg
01_5-27-2010_007.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_007.jpg
01_5-27-2010_002.jpg
./01/01_1968-1969_ElkhartIN/01_5-27-2010_002.jpg

For some reason the first file is not being processed.
It's not empty either:
peter@pm01: $ lt 01/01_1968-1969_ElkhartIN/
total 33480
-rw-rw-r-- 1 peter users 4087690 2010-06-04 15:35 5-27-2010_009.jpg
-rw-rw-r-- 1 peter users 3601483 2010-06-04 15:35 5-27-2010_008.jpg
-rw-rw-r-- 1 peter users 3312665 2010-06-04 15:34 5-27-2010_007.jpg
-rw-rw-r-- 1 peter users 2414998 2010-06-04 15:34 5-27-2010_006.jpg
-rw-rw-r-- 1 peter users 3443748 2010-06-04 15:34 5-27-2010_005.jpg
-rw-rw-r-- 1 peter users 6371661 2010-06-04 15:34 5-27-2010_004.jpg
-rw-rw-r-- 1 peter users 3934255 2010-06-04 15:34 5-27-2010_003.jpg
-rw-rw-r-- 1 peter users 3165457 2010-06-04 15:34 5-27-2010_002.jpg
-rw-rw-r-- 1 peter users 3895062 2010-06-04 15:34 5-27-2010_001.jpg


signature.asc

Tim Nichols

unread,
Jun 18, 2010, 12:48:12 AM6/18/10
to kp...@googlegroups.com
Hi Peter,
I'm assuming that you are simply uncommenting the awk, and commenting
the echo. What's happening is that awk is grabbing STDIN at that
point, and processing the remainder of the output of find. The entire
while loop actually runs in a sub-shell with find's stdout redirected to
the subshells stdin.

So, read f picks up the first line of the output, then awk processes the
remainder of it. What you could do instead is just get rid of your
while loop, and pipe the find command directly to your awk like this:


#!/bin/bash
find . -name '*.[jJ]*' |

awk -F/ '{file=sprintf("%s%s%s",$2,"_",$4); print file; sub($4,
file); sub("JPG", "jpg"); print $0}'


Tim

Peter Martin

unread,
Jun 18, 2010, 12:53:33 AM6/18/10
to kp...@googlegroups.com
Awesome!! Thank you!

Didn't know that was a subshell, but it makes sense!

I was going nuts finding a solution!
signature.asc
Reply all
Reply to author
Forward
0 new messages