The UNIX and Linux Forums  

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 02:06 PM
Tar and moving directories stocksj SUN Solaris 2 11-13-2007 10: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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Jul 2008
Posts: 6
Bash and Awk for creating directories and moving files

I have a security system that FTPs the camera files to my machine, however I want to sort the pictures (taken every 30s) into directories by hour.

Every picture uses the following file format.
yymmddhhmmsstt.jpg (where tt is the milliseconds)

I am thinking the for loop is best

for file in *.jpg
do
?here?
done

I know its very vague, however the end result is I am guessing use awk (or better?) to create variables from the yy mm dd hh parts of the filenames, then creating directories based on those variables nested with a check to see if the directory exists, creating the directory if it doesnt.

i.e. (the way I imagine it working, I dont know how to actually construct the script to do this)

for file in 08072400123200.jpg
do
%y=08
%m=07
%d=24
%h=00
if not exist directory %y - mkdir %y
if not exist directory %y/%m - mkdir %y/%m
if not exist directory %y/%m/%d - mkdir %y/%m/%d
if not exist directory %y/%m/%d/%h - mkdir %y/%m/%d/%h
mv %y%m%d%h*.jpg %y/%m/%d/%h
done

Moving all the pictures taken within that hour into the respective subdirectories.

Any help would be much appreciated.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Sep 2006
Posts: 2,327
using bash, you should start reading up here. It shows you how to get substrings.
Reply With Quote
  #3 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Jul 2008
Posts: 6
What I have so far, that I imagine should work (but doesnt) is (and isnt very elegant or sane)

Code:
#!/bin/bash

for file in *.jpg; do

set yy = `echo $file | awk '{split($0,a,""); print a[1]a[2]}'`
set mm = `echo $file | awk '{split($0,a,""); print a[3]a[4]}'`
set dd = `echo $file | awk '{split($0,a,""); print a[5]a[6]}'`
set hh = `echo $file | awk '{split($0,a,""); print a[7]a[8]}'`

if [ -d $yy ]; then
        if [ -d $yy/$mm ]; then
                if [ -d $yy/$mm/$dd ]; then
                        if [ -d $yy/$mm/$dd/$hh ]; then
                                mv $yy$mm$dd$hh*.jpg $yy/$mm/$dd/$hh/$file
                        elif
                                mkdir $yy/$mm/$dd/$hh
                                exit 1
                        fi
                elif
                        mkdir $yy/$mm/$dd
                        exit 1
                fi
        elif
                mkdir $yy/$mm
                exit 1
        fi
elif
        mkdir $yy
        exit 1
fi

done
Reply With Quote
  #4 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Jul 2008
Posts: 6
Even modifying for various shell stupidity doesnt help

./filter.sh: line 17: syntax error near unexpected token `fi'
./filter.sh: line 17: ` fi'

Code:
#!/bin/bash -x

for file in *.jpg; do

YY = "echo $file | awk '{split($0,a,""); print a[1]a[2]}'"
MM = "echo $file | awk '{split($0,a,""); print a[3]a[4]}'"
DD = "echo $file | awk '{split($0,a,""); print a[5]a[6]}'"
HH = "echo $file | awk '{split($0,a,""); print a[7]a[8]}'"

if [ -d $YY ]; then
        if [ -d $YY/$MM ]; then
                if [ -d $YY/$MM/$DD ]; then
                        if [ -d $YY/$MM/$DD/$HH ]; then
                                mv $YY$MM$DD$HH*.jpg $YY/$MM/$DD/$HH/$file
                        elif
                                mkdir $YY/$MM/$DD/$HH
                        fi
                elif
                        mkdir $YY/$MM/$DD
                fi
        elif
                mkdir $YY/$MM
        fi
elif
        mkdir $YY
fi

done
Reply With Quote
  #5 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Jun 2008
Posts: 40
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
Reply With Quote
  #6 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Jul 2008
Posts: 6
Well, its working (for those out there that stumble across this site for a similar script)

Its not elegant, sane or other ... but it works.

If the better experienced here can clean it up and solve the elegant/sane issues then it would be great

Code:
#!/bin/bash -x

for file in *.jpg; do

YY=`echo $file | awk '{split($0,a,""); print a[1]a[2]}'`
MM=`echo $file | awk '{split($0,a,""); print a[3]a[4]}'`
DD=`echo $file | awk '{split($0,a,""); print a[5]a[6]}'`
HH=`echo $file | awk '{split($0,a,""); print a[7]a[8]}'`

if [ -d $YY ]
then
        if [ -d $YY/$MM ]
        then
                if [ -d $YY/$MM/$DD ]
                then
                        if [ -d $YY/$MM/$DD/$HH ]
                        then
                                mv $YY$MM$DD$HH*.jpg $YY/$MM/$DD/$HH
                        else
                                mkdir $YY/$MM/$DD/$HH
                        fi
                else
                        mkdir $YY/$MM/$DD
                fi
        else
                mkdir $YY/$MM
        fi
else
        mkdir $YY
fi

done
Reply With Quote
  #7 (permalink)  
Old 07-24-2008
danmero danmero is offline Forum Advisor  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 901
Shorther:
Code:
for i in *.jpg;do d=${i:0:8};test -d $d || mkdir $d ;mv $i $d;done
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
None

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 09:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66