Script for creating a directory & move the .tif files in it.

Hi Team,

I have thousands of TIF files which are converted from PDF. Below is a sample of it.

LH9406_BLANCARAMOS_2012041812103210320001.tif
LH9406_BLANCARAMOS_2012041812103210320002.tif
LH9406_BLANCARAMOS_2012041812103210320003.tif
LH9411_ANGENIAHUTCHINSON_2012041812102510250001.tif
LH9411_ANGENIAHUTCHINSON_2012041812102510250002.tif
LH9411_ANGENIAHUTCHINSON_2012041812102510250003.tif
LH9411_ANGENIAHUTCHINSON_2012041812102510250004.tif
LH9411_ANGENIAHUTCHINSON_2012041812102510250005.tif
LH9412_DANIELMATEO_2012041812102310230001.tif
LH9412_DANIELMATEO_2012041812102310230002.tif
LH9414_MICHAELLEWIS_2012041812102010200001.tif
LH9414_MICHAELLEWIS_2012041812102010200002.tif
LH9414_MICHAELLEWIS_2012041812102010200003.tif
LH9414_MICHAELLEWIS_2012041812102010200004.tif

I want to create folders like LHR-001,LHR002,LHR-003.....& w ant to move approximately 50 to 60 images in one folder in such a way that all images of first part of file name should be in one folder.

eg. If first 50 files moves in LHR-001 folder & 50th file is
LH9411_ANGENIAHUTCHINSON_2012041812102510250003.tif then it should move the remaining 2 files (LH9411_...004.tif & LH9411_...005.tif) also in LHR-001. Means count of that folder is 52 & afterwards it goes further to move files in LHR-002.

I have a script which create a folder & moves 50 files in it but that script moves exact 50 files. The problem is, if i run that script the sometimes images from single PDF are split in two folders. Here is my script sample....

cc=50; c=0; folder=1; for i in *; do c=`expr $c + 1`; let "r = $c % $cc" ; if [ $r -eq 0 ] ; then folder=`expr $folder + 1` ; fi ; if [ ! -d $folder ]; then mkdir $folder; fi ; mv "$i" $folder/ ;done

Please help me out.

Thank You.

What Operating System and version do you have and what Shell are you using.

Does your script actually work? It looks like it creates folders called 1 , 2 , 3 etc. . The second and subsequent time the script is run it will go very wrong because it will try redistributing the directories created on the previous run.

How many files are there exactly?
Is there any possibility of exceeding LHR-999 ?

If you are still at design stage, I would avoid having hyphen characters in filenames.

I am working on ubuntu 2.6.20-17-server

Yes my script is working. You are right it creates folders like 1,2,3,4.....

I can't say the exact count for files. Because daily count is different, but there is no possibility of exceeding LHR-999. Maximum it will go upto 500 folders.

In that case I sign off now and recommend that other posters take note.

Why??????????? :confused:

So is LHR-001 only LH9411_ files? or do we unconditionally copy 50 and then finish the current prefix?

It should unconditionally moves 50+ files & then finish the current prefix.

#!/bin/bash

# minimum number of files per directory
cc=50
# directory prefix
dest_prefix=./LHR-
# starting suffix for directory (will be zero padded to 3 spaces)
d=1

for file in *.tif; do
        prefix=${file%%_*}

        # only increment destination if we've reached $cc AND
        #   it's a different prefix
        if ((++c > cc)) && [[ $prefix != $prev_prefix ]]; then
                ((c=1,d++))
        fi

        # zero pad destination directory
        printf -v dest "%s%03d" "$dest_prefix" "$d"

        # create destination directory
        mkdir -p "$dest"

        # verify functions correctly before moving stuff
        echo "[$c/$cc] $file -> $dest"
#       mv "$file" "$dest"

        prev_prefix=$prefix
done
1 Like

Hey neutronscott,

It works perfectly & you can't imagine you save my lot of time.

I have no words, thanks a lot.

Awesome man.........thanks you so much.

Well I just now noticed I create the destination directory every iteration. That's probably best done before the loop and then each increment. Whoops. :slight_smile:

1 Like