Counter based on 10 files or less

there are around 676 files in a directory and I want to batch every 10 and then remaining 7 files as well for a batch pressing job simultaneously.

I'm trying to pick-up every 10 counter'd files -- batch these files and then move on with the next batch.. after resetting the Counters back to 0;

but what if I am left with remaining [1-9] files like for now I have 7 files which are not falling under ==10 rule .

FILECOUNTER=0;
for filex  in `ls`; 
do
let "FILECOUNTER++" 
FILENAMES="$filex\t$FILENAMES"
if [[  $FILECOUNTER  -eq 10  ]]
then
$BATCH_THE_PROCESS  `echo -e $FILENAMES`
FILECOUNTER=0; 
FILENAMES="";
fi
done

This loop is running fine for files till 670, but I'm missing last 7 files which are not accounted for in the if-else

Can someone please suggest to get a listing for every 10 or less ( if there are less than 10 files remaining)

Regards,
NMR

Had you told us what OS and shell you are using, some smart solutions might have been presented by now.

root# uname -a
Linux hippo 3.16.0-8-amd64 #1 SMP Debian 3.16.64-2 (2019-04-01) x86_64 GNU/Linux
root# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root# hostnamectl
   Static hostname: hippo
         Icon name: computer-server
           Chassis: server
        Machine ID: ---------------------------------------
           Boot ID: ---------------------------------------
  Operating System: Debian GNU/Linux 8 (jessie)
            Kernel: Linux 3.16.0-8-amd64
      Architecture: x86-64
root# echo $BASH_VERSION
4.3.30(1)-release

With your recent bash , you have "Arithmetic Expansion" at your finger tips. You don't need the ´ls´ "command substitution". Try

for FN in *; do FL="$FL $FN"; if ! ((++CNT%10)); then echo "executing " $FL; FL=""; fi; done; [ "$FL" ] && echo "executing " $FL
executing  file1 file2 file3 file4 file5 file6 file7 file8 file9 file10
executing  file11 file12 file13 file14 file15 file16 file17 file18 file19 file20
executing  file21 file22 file23 file24 file25 file26 file27

. (Check for residual files added after Jim McNamara's post) Replace the echo with your actual command. You might also want to try e.g.

ls | paste -sd"         \n" | while read LN; do echo "executing " $LN; done
1 Like
parms=""
cnt=0
cd /path/to/files
ls | while read fname
do
     parms="$parms $fname"
     cnt=$(($cnt +1))
     if [ $cnt -eq 10 ] ; then
         batch /path/to/myjobname parms
         cnt=0
         parms=""
     fi
done
# one answer to your question - run any amount of leftovers:
[ $cnt -gt 0 ]  &&   batch /path/to/myjobname $parms

     

Edit: oops Rudi beat me to it....

Using shell arrays:

FL=($(ls))
while (( CNT <= ${#FL[@]})); do echo "executing " ${FL[@]:CNT:10}; ((CNT+=10)); done