#!/bin/bash prefix="sg_" suffix=".csv" linesInFile=`wc -l $1 | cut -d' ' -f1` index=0 dirId=0 maxFiles=5000; linesPerFile=200000; old=0 new=0 while [ $new -le $linesInFile ] do dirName="${prefix}${dirId}" if [ $index -eq 0 ] ; then if [ -e "/home/gmateev/Desktop/test/${dirName}" ] ; then dirId=$((dirId+1)) echo "Directory already exists, incrementing" continue else mkdir $dirName fi fi old=new new=$((new+linesPerFile)) name="${prefix}${index}${suffix}" head --lines=${new} $1 | tail --lines=$linesPerFile > /home/gmateev/Desktop/test/${dirName}/${name}; index=$((index+1)) if [ $index -gt $maxFiles ] ; then dirId=$((dirId+1)) index=0 fi done