Copy specific number of files from on directory to another using Linux

linux, shell

Solution

Something like this could make it:

dir=1
counter=1

for file in spec*
do
   echo "cp $file dir_$dir"
   ((counter++))
   (( $counter%1000 == 1 )) && ((dir++))
done

Explanation

- `dir=1` and `counter=1` are setting the variables.

- `for file in spec*` loops through `spec*` pattern name files.

- `echo "cp $file dir_$dir"` will output like `cp spec123 dir_1` / dir_2, ... I used `echo` so that you can check the behaviour before going ahead and doing the proper `cp`.

- `((counter++))` increments the `counter` variable counter.

- `(( $counter%1000 == 1 )) && ((dir++))` if `$counter` is on a form 1000K+1, increment the value of `$dir`.

Problem

I have a directory containing 8000+ fits files and I am wondering if there is a way to copy increments of them unto other directories such that I could have either 8 directories with about 1000 fits files in them each or 4 directories with 2000 fits files?

Original source

Related problems