Sample Header Ad - 728x90

Creating n folders with equal number of files from a large folder with all files

4 votes
4 answers
457 views
I have a large folder of data files, which I want to copy into subfolders to make a specified number of batches. Right now I count how many files there are and use that to make ranges, like so:
cd /dump
batches=4
files=$(cat /data/samples.txt | wc -l)

echo "$files $batches"
for ((i=0; i<=batches; i++))
do
    mkdir -p merged_batches/batch$i
    rm -f merged_batches/batch$i/*

    ls merged/*.sorted.labeled.bam* |
      head -n $(( $((files/batches)) * $((i+1)) * 2 )) |
      tail -n $((2 * files/batches)) |
      xargs -I {} cp {} merged_batches/batch$i

done
Is there a more convenient way to do this?
Asked by rubberduck (53 rep)
Aug 7, 2025, 03:49 PM
Last activity: Aug 11, 2025, 07:21 PM