Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Shell script to find size of subdirectories

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 12-26-2012
AraR87 AraR87 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 18 May 2015, 1:55 AM EDT
Posts: 50
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 Unix and Linux 12-26-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 7 July 2015, 10:47 PM EDT
Location: Amsterdam
Posts: 10,315
Thanks: 348
Thanked 2,725 Times in 2,437 Posts
wrong forum, moving thread...
    #4  
Old Unix and Linux 12-26-2012
rangarasan's Unix or Linux Image
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 1 July 2015, 11:47 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 Unix and Linux 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 Unix and Linux 12-27-2012
rangarasan's Unix or Linux Image
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 1 July 2015, 11:47 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,
RangaLinux
Sponsored Links
    #7  
Old Unix and Linux 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..... Linux
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 02:34 AM.