Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Put files in directories script

Status
Not open for further replies.

DrD123

IS-IT--Management
Jan 23, 2003
111
US
Hi
I'm trying to find a solution (KSH, PERL, C, etc..) for taking files from a source directory then going through a list of many destination directories and whichever destination directory has the least amount of files in them, put all of the files into that directory.
Each time it runs it should always put the files in the directory with the least amount of files.
The amount of files it's putting in the destination can be one or many.
Thanx
DrD123
 
And what have you tried so far ?
with the least amount of files
number of files or total space ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I've only moved them based on Dir_name and time via CRON. So whatever ones run at this time move to that dir, etc...
I want to be able to move them into the DIR's with the least amount of files, when ever it runs.
I already can list the files, untar and unzip them, it's finding the DIR with the least of files that has me at the moment.
thanx.
DrD
 
Well, if the directories are all under the same parent directory, and you want the directory with the least disk space used (not number of files), then you can do this...
Code:
#!/bin/ksh

PARENT_DIR=/path/to/dirs
SMALLEST=$(du -ko ${PARENT_DIR}|sort -n|head -1|cut -f2)

print "Directory using the least disk space: ${SMALLEST}"
 
This will give you the directory with the least number of files in it...
Code:
#!/bin/ksh

PARENT_DIR=~

SMALLEST=$(find ${PARENT_DIR} -type d -print | while read DIR
        do
                COUNT=$(ls -1 ${DIR}|wc -l)
                print "${COUNT} ${DIR}"
        done | sort -n | head -1 | sed 's/^ *[0-9]* *//g')

print "Directory with the fewest number of files: ${SMALLEST}"
If they aren't all under the same directory, you could do it like this...
Code:
#!/bin/ksh

DIR_FILE=file_containing_dir_names.dat

SMALLEST=$(For DIR in $(<${DIR_FILE})
        do
                COUNT=$(ls -1 ${DIR}|wc -l)
                print "${COUNT} ${DIR}"
        done | sort -n | head -1 | sed 's/^ *[0-9]* *//g')

print "Directory with the fewest number of files: ${SMALLEST}"
 
You have not told us what happens if there are more than one
directories which are both having the least amount of files or
disk space. Would it be more logical just to create a new empty directory instead? What about the next time? it will look like files will be transfered from one directory to another. I just don't find the logic in it.
 
I'm sure you could figure it out, but if there's more that one directory having the least, the [tt]sort[/tt] will probably put them in alphabetical order, so you'd get the one that's got the fewest files and is the first alphabetically. And it probably won't tie for fewest files on the next run.

The reason we're not just creating a new empty directory is because that's not what DrD123 asked for. He didn't say why he needed it, he just said he needed it.
 
Thanx for the info SamBones. I didn't even think of using disk space to find which directory to use. I'll give it a shot.

The reason it was needed is for a program to process the files into a dB, and there are as many programs as directories. So I wanted to spread the load out evenly, because the amount of data that comes in can get backed up if only in a couple directories.

I'll let you know how it turns out.
DrD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top