# Counter based on 10 files or less

**URL:** <https://community.unix.com/t/counter-based-on-10-files-or-less/375481>\
**Category:** Shell Programming and Scripting\
**Created:** [April 27, 2019, 7:31am UTC](https://community.unix.com/t/counter-based-on-10-files-or-less/375481 "2019-04-27T07:31:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![busyboy](https://community.unix.com/letter_avatar/busyboy/32/6_449c533f586939b6e5f246632a46421f.png) [@busyboy](https://community.unix.com/u/busyboy)\
**Post date:** [April 27, 2019, 7:31am UTC](https://community.unix.com/t/counter-based-on-10-files-or-less/375481/1 "2019-04-27T07:31:10Z")

</div>

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 .

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [April 27, 2019, 7:51am UTC](https://community.unix.com/t/counter-based-on-10-files-or-less/375481/2 "2019-04-27T07:51:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![busyboy](https://community.unix.com/letter_avatar/busyboy/32/6_449c533f586939b6e5f246632a46421f.png) [@busyboy](https://community.unix.com/u/busyboy)\
**Post date:** [April 27, 2019, 8:17am UTC](https://community.unix.com/t/counter-based-on-10-files-or-less/375481/3 "2019-04-27T08:17:28Z")

</div>

```nohighlight
root# uname -a
Linux hippo 3.16.0-8-amd64 #1 SMP Debian 3.16.64-2 (2019-04-01) x86_64 GNU/Linux

```

```nohighlight
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/"

```

```nohighlight
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

```

```nohighlight
root# echo $BASH_VERSION
4.3.30(1)-release

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [April 27, 2019, 8:29am UTC](https://community.unix.com/t/counter-based-on-10-files-or-less/375481/4 "2019-04-27T08:29:41Z")

</div>

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

```nohighlight
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.

```nohighlight
ls | paste -sd" \n" | while read LN; do echo "executing " $LN; done

```

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [April 27, 2019, 8:32am UTC](https://community.unix.com/t/counter-based-on-10-files-or-less/375481/5 "2019-04-27T08:32:21Z")

</div>

```nohighlight
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....

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [April 27, 2019, 9:03am UTC](https://community.unix.com/t/counter-based-on-10-files-or-less/375481/6 "2019-04-27T09:03:24Z")

</div>

Using shell arrays:

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

```
