Shell script to find size of subdirectories | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Shell script to find size of subdirectories

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-26-2012
ankit2012 ankit2012 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 8 May 2014, 1:54 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Shell script to find size of subdirectories

Hi
I have to find size of subdirectory and automate it in CRON.

Eg: parent directory name is NVBKP inside it there are several subdirectories
I want to get the size of recent two subdirectories.

I have tried
Code:
ls -ltr diretory path | tail -2

But it is providing only size of the folder not the contents of that folder.

For eg if there are 5 subdirectories in NVBKP say

Code:
a
b
c
d
e

then i need complete size of d and e.
say complete size of d and e is 25 gb
but when i am using
Code:
ls -ltr directory path| tail -2

it is giving me only size of folder d and e i.e 256 mb or something but not its contents.

Kindly suggest.

Last edited by Scrutinizer; 12-26-2012 at 09:32 AM.. Reason: code tags
Sponsored Links
    #2  
Old 12-26-2012
AraR87 AraR87 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 9 July 2014, 8:05 AM EDT
Posts: 46
Thanks: 1
Thanked 2 Times in 2 Posts
try


Code:
ls -lRt

and see

Last edited by Scrutinizer; 12-26-2012 at 09:32 AM.. Reason: code tags
Sponsored Links
    #3  
Old 12-26-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 24 October 2014, 8:44 PM EDT
Location: Amsterdam
Posts: 9,549
Thanks: 285
Thanked 2,426 Times in 2,174 Posts
wrong forum, moving thread...
    #4  
Old 12-26-2012
rangarasan's Avatar
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 2 September 2014, 7:23 AM EDT
Location: Chennai, India
Posts: 496
Thanks: 9
Thanked 124 Times in 120 Posts
du

Hi,
Try this one,

Code:
ls -ltr|awk -v l=2 '/^d/{c++;a[c]=$9;}END{for(i=c-l+1;i<=c;i++){print a[i];}}'|xargs -I '{}' du -sh {}

Cheers,
Ranga:-)

Last edited by rangarasan; 12-27-2012 at 05:01 AM.. Reason: updated
Sponsored Links
    #5  
Old 12-27-2012
ankit2012 ankit2012 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 8 May 2014, 1:54 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Hi
Thanks for the reply


Code:
ls -ltr|tail -2|xargs -I '{}' du -sg {}

.Its giving the following result.


Code:
du: drwxr-xr-x    3 oranvd   dba             256 Dec 16 03:30 20121216: A file or directory in the path name does not exist.
du: drwxr-xr-x    3 oranvd   dba             256 Dec 23 03:30 20121223: A file or directory in the path name does not exist.

Its not giving the desired result these folders has files and the size of the same different but its showing only 256 that is not correct.

Last edited by Scrutinizer; 12-27-2012 at 03:21 AM.. Reason: code tags
Sponsored Links
    #6  
Old 12-27-2012
rangarasan's Avatar
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 2 September 2014, 7:23 AM EDT
Location: Chennai, India
Posts: 496
Thanks: 9
Thanked 124 Times in 120 Posts
du

Hi,

Try this one,



Code:
cd dir;
ls -ltr|awk -v l=2 '/^d/{c++;a[c]=$9;}END{for(i=c-l+1;i<=c;i++){print a[i];}}'|xargs -I '{}' du -sh {}

You can specify the n number of latest files which you want in variable l.

Cheers,
Ranga
Sponsored Links
    #7  
Old 12-27-2012
ankit2012 ankit2012 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 8 May 2014, 1:54 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thankyou so much Ranga
It worked.
Thanks a lot
Cheers.....
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Command for total number of files (and size) across subdirectories? Beun UNIX for Dummies Questions & Answers 9 09-01-2011 11:17 AM
Shell:Find a word in files in a directory and subdirectories yeclota Shell Programming and Scripting 4 08-29-2008 05:05 AM
Shell Script to find the tablespace size in oracle. vighna Shell Programming and Scripting 4 07-26-2008 10:59 PM
shell script to find files by date and size dadadc UNIX for Dummies Questions & Answers 1 10-20-2007 05:18 AM
Shell script to Find file size ragsnovel Shell Programming and Scripting 1 08-10-2007 10:01 AM



All times are GMT -4. The time now is 12:15 AM.