The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #10 (permalink)  
Old 07-24-2008
chihung chihung is offline
Registered User
  
 

Join Date: Jun 2008
Location: Singapore
Posts: 48
I was trying to be 'smart', too many 'short circuit' make me short circuit too.
it should look like this:

Code:
for i in *.jpg
do
   # yy is $1, mm is $2, dd is $3, hh is $4
   set -- `echo $i | sed -e 's/\([0-9][0-9]\)/\1 /g'`
   dir="$1/$2/$3/$4"
   [ ! -d $dir ] && mkdir -p $dir
   mv $i $dir
done
You may even want to ensure *.jpg to be your patten by:
Code:
for i in [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].jpg
do
...
Not sure whether bash has a more elegant way to pick up your files in the for loop, but the above [0-9]... should definitely work for sh.

As for danmero contribution, I couldn't get it working in Solaris bash
Code:
$ i=090807060504.jpg
$ d=${i:0:8}
bad substitution
$ echo $SHELL
/bin/bash
$ uname -a
SunOS chihung 5.10 Generic_118833-36 sun4u sparc SUNW,UltraSPARC-IIi-cEngine
In cygwin, although it does not throw exception, the variable d is equivalent to extracting the first 8 digits. We still need to turn that into directory path before we can make the hierarchical tree structure
Code:
$ i=090807060504.jpg
$ d=${i:0:8}
$ echo $d
09080706
$ uname -a
CYGWIN_NT-5.1 chihung 1.5.25(0.156/4/2) 2007-12-14 19:21 i686 Cygwin
Danmero, did I miss anything