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 and shell scripting languages 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 05:21 AM
Script for checking and reporting file sizes in a directory. marconi Shell Programming and Scripting 1 04-03-2008 09:00 AM
Help on optimization of the script aju_kup UNIX for Dummies Questions & Answers 2 11-20-2007 02:21 AM
Loop through files in a directory rladda Shell Programming and Scripting 4 06-24-2005 06:30 AM
Directory sizes TRUEST UNIX for Dummies Questions & Answers 3 09-24-2003 10:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-16-2008
la_womn la_womn is offline
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.
  #2 (permalink)  
Old 05-16-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
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 {} \;

  #3 (permalink)  
Old 05-16-2008
la_womn la_womn is offline
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?
  #4 (permalink)  
Old 05-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

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

Join Date: Oct 2007
Location: USA
Posts: 753
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 {} \;")

  #6 (permalink)  
Old 05-16-2008
la_womn la_womn is offline
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 06:43 PM..
  #7 (permalink)  
Old 05-17-2008
la_womn la_womn is offline
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0