The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
to compare total directory structure and get sizes of all f on two different servers mannam srinivas Shell Programming and Scripting 3 04-07-2008 01:21 AM
Script for checking and reporting file sizes in a directory. marconi Shell Programming and Scripting 1 04-03-2008 05:00 AM
Help on optimization of the script aju_kup UNIX for Dummies Questions & Answers 2 11-19-2007 10:21 PM
Loop through files in a directory rladda Shell Programming and Scripting 4 06-24-2005 02:30 AM
Directory sizes TRUEST UNIX for Dummies Questions & Answers 3 09-24-2003 06:24 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2007
Posts: 13
Directory sizes loop optimization

I have the following script:

#!/usr/bin/ksh

export MDIR=/datafiles

NAME=$1
SERVER=$2
DIRECTORY=$3
DATABASE=$4
ID=$5

export dirlist=`/usr/bin/ssh -q $ID@$SERVER find $DIRECTORY -type d -print`
for dir in $dirlist
do
SIZE=`</dev/null /usr/bin/ssh -q $ID@$SERVER du -ks $dir`
echo $NAME $DATABASE $SIZE $DIRECTORY>> $MDIR/bldtuout.txt
done


It is running forever, but does return the correct results. Is there a faster way? My goal is to have the directory sizes of all directories under a given path.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 403
Why use the for-loop when it can all be done by find.

Code:
/usr/bin/ssh -q $ID@$SERVER find $DIRECTORY -type d -exec du -sk {} \;
Reply With Quote
  #3 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2007
Posts: 13
So, it should look like this:

#!/usr/bin/ksh

export MDIR=/datafiles

NAME=$1
SERVER=$2
DIRECTORY=$3
DATABASE=$4
ID=$5

SIZE=`/usr/bin/ssh -q $ID@$SERVER find $DIRECTORY -type d -exec du -sk {} \;`
echo $NAME $DATABASE $SIZE $DIRECTORY>> $MDIR/bldtuout.txt
done


When I try to run it, I get find: incomplete statement What am I doing wrong?
Reply With Quote
  #4 (permalink)  
Old 05-16-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,501
You probably need to double (or triple or quadruple) the backslash in order for the remote ssh to receive it correctly.
Reply With Quote
  #5 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 403
Put it in double-quotes and use $(cmd) instead of `cmd` which makes it much easier to follow.

Code:
SIZE=$(/usr/bin/ssh -q $ID@$SERVER "find $DIRECTORY -type d -exec du -sk {} \;")
Reply With Quote
  #6 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2007
Posts: 13
Thank you! That did it.

Two more questions:
Now the output is all on one line, is there a quick way to parse it to multiple lines?

When the find creates an error, it is written to the screen, is there a way to get it writen to an output file?

Last edited by la_womn; 05-16-2008 at 02:43 PM.
Reply With Quote
  #7 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2007
Posts: 13
I figured it out. My script now looks like:

Quote:
#!/usr/bin/ksh

export MDIR=$PS_HOME/datafiles

NAME=$1
SERVER=$2
DIRECTORY=$3
DATABASE=$4
ID=$5

echo "*" $NAME $DATABASE $DIRECTORY >> $MDIR/bldtuout.txt
/usr/bin/ssh -q $ID@$SERVER "find $DIRECTORY -type d -exec du -ks {} \;" >> $MDIR/bldtuout.txt 2> $MDIR/bldterr.txt
Thank yiou to everyone for your help.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0