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
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.
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.
#!/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