I have some directories with thousands of files and I need to split those files into directories of 4,000 files or less. I'm fairly new to scripting so please bear with me. I've searched for a solution and I see that you can copy ALL files from a directory to a new directory but I want to copy only some of the files. All the files are the same type.
#split files into different directories if directory has more than 4,000 files
if $totalNumOfFiles > 4000
#Determine how many new directories are needed
numOfNewDir = int($totalNumOfFiles/4000)
#The new directories will be like this: original_folder_name_1, original_folder_name_2, etc.
#depending on how many new directories have to be created
sourceDirName = $DIR
while [ $numOfNewDir > 0 ]
destinationDirName = $sourceDirName_$numOfNewDir
#this is where I would split the files and copy them into a new directory but I don't know how to do it
#start copying 4,000 files
#cp $sourceDirName/files to $destinationDirName/files
#end copying, the next iteration would copy the next set of files starting at file number 4001