Unix Programming - Folder contents

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > Folder contents





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Folder contents
yorkyboy

2005-05-06, 6:02 pm

Guys im very new to Unix and scripting as a whole, but can anyone tell
me if its posible to create a script that looks at say 4 folders, then
checks which has the least amount of files in, then copy a file to that
folder?.

Fletcher Glenn

2005-05-06, 6:02 pm

yorkyboy wrote:
> Guys im very new to Unix and scripting as a whole, but can anyone tell
> me if its posible to create a script that looks at say 4 folders, then
> checks which has the least amount of files in, then copy a file to that
> folder?.
>


Sure it's possible. Why don't you show us your approach so far? We'll
then try to help.

--

Fletcher Glenn

SM Ryan

2005-05-06, 6:03 pm

"yorkyboy" <dean.ward@intouchplc.com> wrote:
# Guys im very new to Unix and scripting as a whole, but can anyone tell
# me if its posible to create a script that looks at say 4 folders, then
# checks which has the least amount of files in, then copy a file to that
# folder?.

You can do something like
target=$(du foldername1 foldername2 ... | sort -n | cut -f 2 | sed '2,$d')
cp file $target

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Haven't you ever heard the customer is always right?
yorkyboy

2005-05-11, 2:49 am

im trying to create a script that will load ballance 3 devices. Unix is
still very alien to me having only been in this position for 3 months
now. Not to mention the fact that i've been thrown in the deep end.
Anyway so far i've managed to make a script that looks at all 4 devices
and make a report:

ls ~/Desktop/printer1/ ~/Desktop/printer2/ ~/Desktop/printer3/ >
~/Desktop/contents.txt

This gives me:
=A0/Users/Desktop/printer1/:
Job1
Job2
job4

/Users/Desktop/printer2/:
Job1
Job2
Job3

/Users/Desktop/printer3/:
Job1

>From this i'd like to create a script that will now copy a file to the

emptiest folder.

Bjorn Reese

2005-05-11, 5:56 pm

yorkyboy wrote:
> im trying to create a script that will load ballance 3 devices. Unix is

[...]
> emptiest folder.


Try something like the following script. When you execute the script
you first specify the file name, and then all the directories. E.g.

% yourscriptname yourfile ~/Desktop/printer1/ ~/Desktop/printer2/
~/Desktop/printer3/

You can also hardcode the directory names into the script by
replacing the $* in the for loop with a variable name containing
the directories.


#!/bin/sh
# Usage: yourscriptname file dir1 dir2 ... dirN
FILE=$1
shift
CURRENT_COUNT=99999
CURRENT_DIR=""
for DIR in $*; do
COUNT=`ls -1 $DIR | wc -l`
if [ $COUNT -lt $CURRENT_COUNT ]; then
CURRENT_COUNT=$COUNT
CURRENT_DIR=$DIR
fi
done
if [ -n $CURRENT_DIR ]; then
cp $FILE $CURRENT_DIR
fi

--
mail1dotstofanetdotdk
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com