Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I am pretty new to scripting and i need to create a directory structure and populate it with data from a list of file names & a dource directory ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Posts
    4

    Simple scripting help.....


    Hi,

    I am pretty new to scripting and i need to create a directory structure and populate it with data from a list of file names & a dource directory containing all of the file names.

    For example, say i have 1000 images, all with a 5 digit file name appended with .jpg.
    E.G:
    12345.jpg

    Will be split out so that it will create the directory structure /image/12/34/ and the image file 12345.jpg will be placed in the final 34 directory. As will any other image that begins 1234 I.E 12341.jpg will go to the same location.

    Im not really looking for someone to post up the code that will do this. More a pointer into how they would do it in theory. This is my first real attempt at a script and i had manged to create the directory structure ok. It reads through the list of images and using cut and loops creates all of the combinations of directories required. What i am sturggling with is how i can then get the script to read the file name and place it in the relevant directory.

    I.E from the images in the source directory it identifies all of the files beginning with 1234 and places them in the directory structure /images/12/34/ equally for a image named 45678.jpg it places it in /images/45/67/

    May be something nice and simple that i am over complicating but any pointers would be appreciated.

    Thanks,

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    I suppose you are using bash.

    ${file:a:b}

    retrieves characters form index a to b from file variable. Thus you can get the 1st and 2nd, and the 3rd and 4th ones to construct you destination path in your script.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Just Joined!
    Join Date
    Feb 2010
    Posts
    4
    Thank you for your quick reply, i am hoping that this will help me immencely however from this alone the penny has not quite dropped..... I obviously need to RTFM a little more. Is there a man page that you can think of that may help me understand the little string you have there?

    Or an example you could give that may help me understand how to tie that logic into my script?

    You are correct to assume that this is bash, somehow i have managed to bumble along happily in IT for 2-3 years without having to touch upon scripting. Getting my head round it all is proving extremely difficult at the minute but once i crack it (i know it will be a perminant learning curve from here on) i think that this could make my working day a lot more enjoyable.

    Thanks again,

  4. #4
    Just Joined! sathiya's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    97
    Code:
    $ ls
    12341.jpg  12345.jpg  43561.jpg  t1.sh
    This script is what you are looking for,
    Code:
    $ cat t1.sh
    for f in $( ls *.jpg )
    do
            #echo $f
            filename=$f
    
            dir1=${f:0:2}
            #echo $dir1
    
            dir2=${f:2:2}
            #echo $dir2
    
            echo "mv $f /images/$dir1/$dir2/"
    done
    Code:
    $ bash t1.sh
    mv 12341.jpg /images/12/34/
    mv 12345.jpg /images/12/34/
    mv 43561.jpg /images/43/56/

  5. #5
    Just Joined!
    Join Date
    Feb 2010
    Posts
    4
    Thank you very much for this Sathiya - this does indeed look like it will do exactly what i require. I do have one issue whereby the amount of images is too many for 'ls' to handle but i can get around this by splitting the source directory into subdirectories i would have thought.

    No doubt you will see me a lot more on these forums as i get my teeth into more scripting tasks. Hopefully one day it will be me passing on advice to the beginners!

  6. #6
    Just Joined!
    Join Date
    Feb 2010
    Posts
    4
    For info i got around the following error:

    /usr/bin/ls: The parameter or environment lists are too long.

    by changing the:

    ls *.jpg

    step to

    ls |grep "\.jpg$"

    & this seems to have resolved the issue. Thanks for all of your help.

  7. #7
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •