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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
creating directories on the same box vivek_damodaran HP-UX 3 11-14-2007 03:06 PM
Tar and moving directories stocksj SUN Solaris 2 11-13-2007 11:33 AM
moving directories to new directories on multiple servers mackdaddy07 Shell Programming and Scripting 0 04-06-2007 11:30 AM
bash/awk scripting help (creating OLD new users) Jukai Shell Programming and Scripting 2 10-17-2006 05:36 AM
creating directories carlvernon UNIX for Dummies Questions & Answers 3 06-01-2006 01:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #8 (permalink)  
Old 07-24-2008
Kiint Kiint is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 6
Quote:
Originally Posted by chihung View Post
We do not have to test for individual directory level 'cos if the bottom directory exists, the parent level should exist. Also, mkdir -p will make all the non-existing parent directories. Also, I introduce 'short circurt' && to ensure directory exist before I move the file.

We can also avoid all the repeating code using in extracting yy/mm/dd/hh by using 'set --' and sed. sed will change 2 digits with 2 digits + space so that it can put the result back to "set --" to set the positional variables accordingly

This is my contribution, it should work (even on sh)
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
Your script is absolutely perfect, I love it ... except ...

the last --> [ ! -d $dir ] && mkdir -p $dir && mv $i $dir <-- wouldnt actually move the file if the directory existed already, so I added an extra line below with just --> mv $i $dir <-- in place and that solved that part, so it now becomes, although I would imagine this will now generate an error if the directory it wants to create doesnt exist (but shouldnt error for subsequent files - I think)

Code:
#!/bin/bash -x

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
   mv $i $dir
done
  #9 (permalink)  
Old 07-24-2008
Kiint Kiint is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 6
Quote:
Originally Posted by danmero View Post
Shorther:
Code:
for i in *.jpg;do d=${i:0:8};test -d $d || mkdir $d ;mv $i $d;done
Awesome single line version, perfect except the lack of nested directories.

Instead of a single directory 08072511 I need them nested like

>08
->07
-->25
--->11
  #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
  #11 (permalink)  
Old 07-24-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,369
Ops, fast reading
Code:
for i in *.jpg;do d=.$(sed 's/\(..\)/\/\1/g' <<< ${i:0:8});test -d $d || mkdir -p $d ;mv $i $d;done
@chihung
Code:
$ uname -a
Linux test 2.6.18-6-686 #1 SMP Sun Feb 10 22:11:31 UTC 2008 i686 GNU/Linux

Last edited by danmero; 07-24-2008 at 10:22 PM..
  #12 (permalink)  
Old 07-24-2008
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,402
Try...
Code:
for i in *.jpg; do d=$(echo $i|cut -c-8|fold -2|paste -s -d /); mkdir -p $d; mv $i $d; done
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0