Solved

How to split files in one directory into 2 or more directories?

Posted on 2015-01-09
5
273 Views
Last Modified: 2015-01-12
Hi all!

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 ]
do

destinationDirName = $sourceDirName_$numOfNewDir
mkdir $destinationDirName

#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

--numOfNewDir
done


done
0
Comment
Question by:afrocelt
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40541540
sourceDirName=$DIR
files=( $sourceDirName/* )
if (( ${#files[*]} > 4000 )) ; then
  for (( i = 0 ; i < ${#files[@]} ; i++ )) do
    destinationDirName=${sourceDirName}_$((1+$i/4000))
    if [[ ! -d $destinationDirName ]] ; then  mkdir $destinationDirName ; fi
    mv "${files[$i]}" "$destinationDirName/"
  done
fi
0
 

Author Comment

by:afrocelt
ID: 40545348
Hi Ozo,

Thank you so much for the quick reply. I almost have it working except when the path contains spaces. I know I need to put quotes somewhere along here:

destinationDirName=${sourceDirName}_$((1+$i/4000))
f [[ ! -d $destinationDirName ]] ; then  mkdir $destinationDirName ;

because it tries multiple times to create a directory where there is a space:

If the directory is:  /Volumes/DirA/DirB/Personal Folders/me/THE FILES/image1.JPG

I get this:
mkdir: /Volumes/DirA/DirB/Personal: File exists
mkdir: Folders/me: No such file or directory
mkdir: FILES_5: File exists

Strangely, it still creates the correct path:

/Volumes/DirA/DirB/Personal Folders/me/THE FILES_5/

Do I need to put quotes around ${sourceDirName}? I tried a few iterations but I think I'm doing something wrong. Thanks in advance.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40545355
files=( "$sourceDirName"/* )
if (( ${#files[*]} > 4000 )) ; then
  for (( i = 0 ; i < ${#files[@]} ; i++ )) do
    destinationDirName=${sourceDirName}_$((1+$i/4000))
    if [[ ! -d "$destinationDirName" ]] ; then  mkdir "$destinationDirName" ; fi
    mv "${files[$i]}" "$destinationDirName/"
  done
fi
0
 

Author Comment

by:afrocelt
ID: 40545393
Yes! That worked. I really appreciate your help.
0
 

Author Closing Comment

by:afrocelt
ID: 40545396
The response was quick as well as the answer to my follow-up question. Really helpful!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question